Skip to content

Commit 6f6d522

Browse files
auth-templating (#483)
Summary: - Support for `account_id` `auth` DTO attribute. - `golang` templating supported for `token_url` field **only** in `auth` DTO. - Inline templating to be used by data transfer objects. Eg `{{ .my_attribute }}` will dereference vallue serialized at `my_attribute` in given structure. - Reading from environment using the prefix `__env__`; eg: `{{ .__env__MY_VAR }}` will dereference the env var `MY_VAR`. - Auth DTO now supports `account_id` and `account_id_env_var` attributes. These are not standards-aligned, semantics can vary. - Amended test materials for robot test `Oauth2 CLient Credentials Auth Should Succeed with Valid Config`, in order to cover off new functionality.
1 parent 0d4c37c commit 6f6d522

File tree

8 files changed

+19
-5
lines changed

8 files changed

+19
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/spf13/cobra v1.4.0
2222
github.com/spf13/pflag v1.0.5
2323
github.com/spf13/viper v1.10.1
24-
github.com/stackql/any-sdk v0.0.3-beta21
24+
github.com/stackql/any-sdk v0.0.3-beta27
2525
github.com/stackql/go-suffix-map v0.0.1-alpha01
2626
github.com/stackql/psql-wire v0.1.1-alpha07
2727
github.com/stackql/stackql-parser v0.0.14-alpha04

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
471471
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
472472
github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk=
473473
github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8qy1rU=
474-
github.com/stackql/any-sdk v0.0.3-beta21 h1:1x76S9scXukHKcBUmzSVYpwWG8TnZXMhlgU0HHcTO2g=
475-
github.com/stackql/any-sdk v0.0.3-beta21/go.mod h1:CIMFo3fC2ScpqzkzeCkzUQQuzYA1VuqpG0p1EZXN+wY=
474+
github.com/stackql/any-sdk v0.0.3-beta27 h1:WRfGfseXdHcQUD+dXNH2+W7HjzhHzX/JPstmh31JW+0=
475+
github.com/stackql/any-sdk v0.0.3-beta27/go.mod h1:CIMFo3fC2ScpqzkzeCkzUQQuzYA1VuqpG0p1EZXN+wY=
476476
github.com/stackql/go-suffix-map v0.0.1-alpha01 h1:TDUDS8bySu41Oo9p0eniUeCm43mnRM6zFEd6j6VUaz8=
477477
github.com/stackql/go-suffix-map v0.0.1-alpha01/go.mod h1:QAi+SKukOyf4dBtWy8UMy+hsXXV+yyEE4vmBkji2V7g=
478478
github.com/stackql/psql-wire v0.1.1-alpha07 h1:LQWVUlx4Bougk6dztDNG5tmXxpIVeeTSsInTj801xCs=

internal/stackql/dto/auth_ctx.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ type AuthCtx struct {
5151
ClientSecretEnvVar string `json:"client_secret_env_var" yaml:"client_secret_env_var"`
5252
Values url.Values `json:"values" yaml:"values"`
5353
AuthStyle int `json:"auth_style" yaml:"auth_style"`
54+
AccountID string `json:"account_id" yaml:"account_id"`
55+
AccoountIDEnvVar string `json:"account_id_env_var" yaml:"account_id_var"`
5456
}
5557

5658
func (ac *AuthCtx) GetSQLCfg() (SQLBackendCfg, bool) {
@@ -96,6 +98,8 @@ func (ac *AuthCtx) Clone() *AuthCtx {
9698
ClientSecretEnvVar: ac.ClientSecretEnvVar,
9799
Values: ac.Values,
98100
AuthStyle: ac.AuthStyle,
101+
AccountID: ac.AccountID,
102+
AccoountIDEnvVar: ac.AccoountIDEnvVar,
99103
}
100104
return rv
101105
}

internal/stackql/handler/handler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ func transformOpenapiStackqlAuthToLocal(authDTO anysdk.AuthDTO) *dto.AuthCtx {
583583
ClientSecretEnvVar: authDTO.GetClientSecretEnvVar(),
584584
Values: authDTO.GetValues(),
585585
AuthStyle: authDTO.GetAuthStyle(),
586+
AccountID: authDTO.GetAccountID(),
587+
AccoountIDEnvVar: authDTO.GetAccountIDEnvVar(),
586588
}
587589
successor, successorExists := authDTO.GetSuccessor()
588590
currentParent := rv

internal/stackql/provider/auth_util.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77

8+
"github.com/stackql/any-sdk/pkg/litetemplate"
89
"github.com/stackql/stackql/internal/stackql/constants"
910
"github.com/stackql/stackql/internal/stackql/dto"
1011
"github.com/stackql/stackql/internal/stackql/netutils"
@@ -199,11 +200,15 @@ func getGenericClientCredentialsConfig(authCtx *dto.AuthCtx, scopes []string) (*
199200
if secretErr != nil {
200201
return nil, secretErr
201202
}
203+
templatedTokenURL, templateErr := litetemplate.RenderTemplateFromSerializable(authCtx.GetTokenURL(), authCtx)
204+
if templateErr != nil {
205+
return nil, fmt.Errorf("incorrect token url templating %w", templateErr)
206+
}
202207
rv := &clientcredentials.Config{
203208
ClientID: clientID,
204209
ClientSecret: clientSecret,
205210
Scopes: scopes,
206-
TokenURL: authCtx.GetTokenURL(),
211+
TokenURL: templatedTokenURL,
207212
}
208213
if len(authCtx.GetValues()) > 0 {
209214
rv.EndpointParams = authCtx.GetValues()

test/registry/src/stackql_oauth2_testing/v0.1.0/provider.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ config:
2727
client_secret_env_var: 'YOUR_OAUTH2_CLIENT_SECRET_ENV_VAR'
2828
type: "oauth2"
2929
grant_type: "client_credentials"
30-
token_url: 'http://localhost:2091/contrived/simple/token'
30+
token_url: 'http://localhost:2091/{{ .__env__YOUR_OAUTH2_SOME_SYSTEM_ACCOUNT_ID }}/simple/token'
3131
scopes:
3232
- 'scope-01'
3333
- 'scope-02'

test/robot/functional/stackql_mocked_from_cmd_line.robot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4192,6 +4192,7 @@ Custom Auth Linear Should Send Appropriate Credentials
41924192
Oauth2 CLient Credentials Auth Should Succeed with Valid Config
41934193
Set Environment Variable YOUR_OAUTH2_CLIENT_ID_ENV_VAR dummy-client-id
41944194
Set Environment Variable YOUR_OAUTH2_CLIENT_SECRET_ENV_VAR dummy-client-secret
4195+
Set Environment Variable YOUR_OAUTH2_SOME_SYSTEM_ACCOUNT_ID contrived
41954196
${outputStr} = Catenate SEPARATOR=\n
41964197
... |-----------|---------|
41974198
... |${SPACE}${SPACE}${SPACE}${SPACE}id${SPACE}${SPACE}${SPACE}${SPACE}${SPACE}|${SPACE}${SPACE}name${SPACE}${SPACE}${SPACE}|

test/robot/lib/StackQLInterfaces.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ def _get_default_env(self) -> dict:
324324
rv["YOUR_OAUTH2_CLIENT_ID_ENV_VAR"] = os.environ.get('YOUR_OAUTH2_CLIENT_ID_ENV_VAR')
325325
if os.environ.get('YOUR_OAUTH2_CLIENT_SECRET_ENV_VAR') is not None:
326326
rv["YOUR_OAUTH2_CLIENT_SECRET_ENV_VAR"] = os.environ.get('YOUR_OAUTH2_CLIENT_SECRET_ENV_VAR')
327+
if os.environ.get('YOUR_OAUTH2_SOME_SYSTEM_ACCOUNT_ID') is not None:
328+
rv["YOUR_OAUTH2_SOME_SYSTEM_ACCOUNT_ID"] = os.environ.get('YOUR_OAUTH2_SOME_SYSTEM_ACCOUNT_ID')
327329
return rv
328330

329331

0 commit comments

Comments
 (0)