Skip to content

Commit

Permalink
⭐️ use endpoint url from service account (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock authored Nov 14, 2023
1 parent e876aab commit 692d09c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
8 changes: 4 additions & 4 deletions internal/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ func privateKeyFromBytes(bytes []byte) (*ecdsa.PrivateKey, error) {
}
}

func NewServiceAccountTokenSource(data []byte) (*serviceAccountTokenSource, error) {
func NewServiceAccountTokenSource(data []byte) (*serviceAccountTokenSource, *serviceAccountCredentials, error) {
var credentials *serviceAccountCredentials
err := json.Unmarshal(data, &credentials)
if credentials == nil || err != nil {
return nil, errors.New("valid service account needs to be provided")
return nil, nil, errors.New("valid service account needs to be provided")
}

// verify that we can read the private key
privateKey, err := privateKeyFromBytes([]byte(credentials.PrivateKey))
if err != nil {
return nil, errors.New("cannot load retrieved key: " + err.Error())
return nil, nil, errors.New("cannot load retrieved key: " + err.Error())
}

// configure authentication plugin, since the server only accepts authenticated calls
Expand All @@ -75,7 +75,7 @@ func NewServiceAccountTokenSource(data []byte) (*serviceAccountTokenSource, erro

return &serviceAccountTokenSource{
cfg: cfg,
}, nil
}, credentials, nil
}

type tokenSourceConfig struct {
Expand Down
22 changes: 13 additions & 9 deletions option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,46 @@ func (w withHTTPClient) Apply(o *internal.DialSettings) {

// WithTokenSource returns a ClientOption that specifies the oauth2.TokenSource
func WithTokenSource(s oauth2.TokenSource) ClientOption {
return withTokenSource{s, nil}
return withTokenSource{"", s, nil}
}

type withTokenSource struct {
ts oauth2.TokenSource
err error
endpoint string
ts oauth2.TokenSource
err error
}

func (w withTokenSource) Apply(o *internal.DialSettings) {
o.TokenSource = w.ts
o.TokenError = w.err
if w.endpoint != "" {
o.Endpoint = w.endpoint + "/query"
}
}

// WithAPIToken returns a ClientOption that specifies the oauth2.TokenSource with the given token.
func WithAPIToken(token string) ClientOption {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
return withTokenSource{src, nil}
return withTokenSource{"", src, nil}
}

// WithServiceAccount returns a ClientOption that specifies the credentials file to use.
func WithServiceAccount(data []byte) ClientOption {
ts, err := signer.NewServiceAccountTokenSource(data)
return withTokenSource{ts, err}
ts, sa, err := signer.NewServiceAccountTokenSource(data)
return withTokenSource{sa.ApiEndpoint, ts, err}
}

// WithServiceAccountFile returns a ClientOption that specifies the credentials file to use.
func WithServiceAccountFile(filename string) ClientOption {
data, err := os.ReadFile(filename)
if err != nil {
return withTokenSource{nil, err}
return withTokenSource{"", nil, err}
}

ts, err := signer.NewServiceAccountTokenSource(data)
return withTokenSource{ts, err}
ts, sa, err := signer.NewServiceAccountTokenSource(data)
return withTokenSource{sa.ApiEndpoint, ts, err}
}

// WithoutAuthentication returns a ClientOption that disables authentication.
Expand Down

0 comments on commit 692d09c

Please sign in to comment.