Skip to content

Commit 2b7986f

Browse files
committed
feat(oauth): use oauth to unify service-to-service authentication.
1 parent be1cd84 commit 2b7986f

9 files changed

Lines changed: 25 additions & 34 deletions

File tree

api/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (k KVPair) String() string {
169169
return k.Name + "=" + k.Value
170170
}
171171

172-
// ExecProbe executes a command within a Pod.
172+
// ExecAction describes a command to execute within a Pod for probing.
173173
type ExecAction struct {
174174
Command []string `json:"command"`
175175
}

apps/apps.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@ func List(c *drycc.Client, workspace string, results int) (api.Apps, int, error)
3030
//
3131
// If the app name already exists, the error drycc.ErrDuplicateApp will be returned.
3232
func New(c *drycc.Client, appID string, workspace string) (api.App, error) {
33-
body := []byte{}
34-
3533
req := api.AppCreateRequest{ID: appID, Workspace: workspace}
36-
b, err := json.Marshal(req)
34+
body, err := json.Marshal(req)
3735
if err != nil {
3836
return api.App{}, err
3937
}
40-
body = b
4138

4239
res, reqErr := c.Request("POST", "/v2/apps/", body)
4340
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {

config/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func TestConfigSet(t *testing.T) {
223223
}
224224

225225
expected := api.Config{
226-
App: "example-go",
226+
App: "example-go",
227227
Values: []api.ConfigValue{
228228
{
229229
Group: "global",
@@ -395,7 +395,7 @@ func TestConfigList(t *testing.T) {
395395
}
396396

397397
expected := api.Config{
398-
App: "example-go",
398+
App: "example-go",
399399
Values: []api.ConfigValue{
400400
{
401401
Group: "global",

drycc.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ type Client struct {
8181

8282
// Token is used to authenticate the request against the API.
8383
Token string
84-
85-
// ServiceKey is the controller token used with the hooks resource.
86-
// The hooks resource isn't intended to be used by users, so it requires
87-
// a service token rather than a user token.
88-
ServiceKey string
8984
}
9085

9186
// APIVersion is the api version compatible with the SDK.

hooks/hooks_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func TestConfigHook(t *testing.T) {
161161
}
162162

163163
expected := api.Config{
164-
App: "example-go",
164+
App: "example-go",
165165
Values: []api.ConfigValue{
166166
{
167167
Group: "global",

http.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ func createHTTPClient(sslVerify bool) *http.Client {
2626
// following policy (such as redirects, cookies, auth) as configured on the client.
2727
func (c *Client) Do(req *http.Request) (*http.Response, error) {
2828
if c.Token != "" {
29-
req.Header.Add("Authorization", "token "+c.Token)
29+
if strings.HasPrefix(strings.ToLower(c.Token), "bearer ") || strings.HasPrefix(strings.ToLower(c.Token), "token ") {
30+
req.Header.Add("Authorization", c.Token)
31+
} else {
32+
req.Header.Add("Authorization", "token "+c.Token)
33+
}
3034
}
3135

32-
if c.ServiceKey != "" {
33-
req.Header.Add("X-Drycc-Service-Key", c.ServiceKey)
34-
}
3536
if req.Header.Get("Content-Type") == "" {
3637
req.Header.Add("Content-Type", "application/json")
3738
}

http_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,6 @@ func (f fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
6868
return
6969
}
7070

71-
bT := "testing"
72-
if req.Header.Get("X-Drycc-Service-Key") != bT {
73-
fmt.Printf("Hook Token Wrong: Expected %s, Got %s\n", bT, req.Header.Get("X-Drycc-Service-Key"))
74-
res.WriteHeader(http.StatusInternalServerError)
75-
res.Write(nil)
76-
return
77-
}
78-
7971
eC := "application/json"
8072
if req.Header.Get("Content-Type") != eC {
8173
fmt.Printf("Content Type Wrong: Expected %s, Got %s\n", eC, req.Header.Get("Content-Type"))
@@ -160,7 +152,6 @@ func TestBasicRequest(t *testing.T) {
160152
t.Fatal(err)
161153
}
162154
drycc.UserAgent = "test"
163-
drycc.ServiceKey = "testing"
164155

165156
res, err := drycc.Request("POST", "/request/", []byte("test"))
166157
if err != nil {

ps/ps.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"net/url"
99
"sort"
10+
"strings"
1011

1112
drycc "github.com/drycc/controller-sdk-go"
1213
"github.com/drycc/controller-sdk-go/api"
@@ -41,10 +42,13 @@ func Exec(c *drycc.Client, appID, podID string, command api.Command) (*websocket
4142
if err != nil {
4243
return nil, err
4344
}
45+
authHeader := c.Token
46+
if !strings.HasPrefix(strings.ToLower(authHeader), "bearer ") && !strings.HasPrefix(strings.ToLower(authHeader), "token ") {
47+
authHeader = "token " + authHeader
48+
}
4449
config.Header = http.Header{
45-
"User-Agent": {c.UserAgent},
46-
"Authorization": {"token " + c.Token},
47-
"X-Drycc-Service-Key": {c.ServiceKey},
50+
"User-Agent": {c.UserAgent},
51+
"Authorization": {authHeader},
4852
}
4953
conn, err := websocket.DialConfig(config)
5054
if err != nil {
@@ -67,10 +71,13 @@ func Logs(c *drycc.Client, appID, podID string, request api.PodLogsRequest) (*we
6771
if err != nil {
6872
return nil, err
6973
}
74+
authHeader := c.Token
75+
if !strings.HasPrefix(strings.ToLower(authHeader), "bearer ") && !strings.HasPrefix(strings.ToLower(authHeader), "token ") {
76+
authHeader = "token " + authHeader
77+
}
7078
config.Header = http.Header{
71-
"User-Agent": {c.UserAgent},
72-
"Authorization": {"token " + c.Token},
73-
"X-Drycc-Service-Key": {c.ServiceKey},
79+
"User-Agent": {c.UserAgent},
80+
"Authorization": {authHeader},
7481
}
7582
conn, err := websocket.DialConfig(config)
7683
if err != nil {

volumes/volumes_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ func TestVolumeMount(t *testing.T) {
409409
}
410410

411411
expected := api.Volume{
412-
Name: "myvolume",
413-
App: "example-go",
412+
Name: "myvolume",
413+
App: "example-go",
414414
Path: map[string]any{
415415
"cmd": "/data/cmd1",
416416
"web": "/data/web1",

0 commit comments

Comments
 (0)