Skip to content

Commit 0f4f526

Browse files
author
Liam Cervante
authored
stacks: add support for authenticating directly via token (#262)
* stacks: add support for authenticating directly via token * add basic test and changelog entry
1 parent f1e251d commit 0f4f526

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

.changelog/262.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
IdentityProviderConfig accepts a workload credential directly.
3+
```

auth/workload/provider.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ type IdentityProviderConfig struct {
2929
// File sources the subject credential from a file.
3030
File *FileCredentialSource `json:"file,omitempty"`
3131

32+
// Token sources the credentials from a directly supplied token.
33+
Token *CredentialTokenSource `json:"token,omitempty"`
34+
3235
// EnvironmentVariable sources the subject credential from an environment
3336
// variable.
3437
EnvironmentVariable *EnvironmentVariableCredentialSource `json:"env,omitempty"`
@@ -59,6 +62,13 @@ func (c *IdentityProviderConfig) Validate() error {
5962
}
6063
}
6164

65+
if c.Token != nil {
66+
set++
67+
if err := c.Token.Validate(); err != nil {
68+
return err
69+
}
70+
}
71+
6272
if c.EnvironmentVariable != nil {
6373
set++
6474
if err := c.EnvironmentVariable.Validate(); err != nil {
@@ -129,6 +139,8 @@ func New(c *IdentityProviderConfig) (*Provider, error) {
129139
p.jwtProvider = c.File
130140
} else if c.EnvironmentVariable != nil {
131141
p.jwtProvider = c.EnvironmentVariable
142+
} else if c.Token != nil {
143+
p.jwtProvider = c.Token
132144
}
133145

134146
return p, nil

auth/workload/token.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package workload
2+
3+
import "fmt"
4+
5+
// CredentialTokenSource sources credentials via a directly supplied token.
6+
type CredentialTokenSource struct {
7+
Token string `json:"token,omitempty"`
8+
}
9+
10+
// Validate validates the config.
11+
func (ct *CredentialTokenSource) Validate() error {
12+
if ct.Token == "" {
13+
return fmt.Errorf("token must be set")
14+
}
15+
16+
return nil
17+
}
18+
19+
// token returns the token.
20+
func (ct *CredentialTokenSource) token() (string, error) {
21+
return ct.Token, nil
22+
}

auth/workload/token_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package workload
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestCredentialTokenSource_Validate(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
ct *CredentialTokenSource
13+
errStr string
14+
}{
15+
{
16+
name: "empty token",
17+
ct: &CredentialTokenSource{},
18+
errStr: "token must be set",
19+
},
20+
}
21+
for _, test := range tests {
22+
t.Run(test.name, func(t *testing.T) {
23+
err := test.ct.Validate()
24+
if test.errStr == "" {
25+
require.NoError(t, err)
26+
} else {
27+
require.ErrorContains(t, err, test.errStr)
28+
}
29+
})
30+
}
31+
}

0 commit comments

Comments
 (0)