Skip to content

Commit 960a8fc

Browse files
committed
add static token
Signed-off-by: Jorge Turrado <[email protected]>
1 parent 39961b0 commit 960a8fc

File tree

4 files changed

+63
-334
lines changed

4 files changed

+63
-334
lines changed

core/auth/auth.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,9 @@ func WorkloadIdentityFederationAuth(cfg *config.Configuration) (http.RoundTrippe
238238
TokenUrl: cfg.TokenCustomUrl,
239239
BackgroundTokenRefreshContext: cfg.BackgroundTokenRefreshContext,
240240
ClientID: cfg.ServiceAccountEmail,
241-
FederatedTokenFilePath: cfg.WorkloadIdentityFederationFederatedTokenPath,
242-
TokenExpiration: cfg.WorkloadIdentityFederationTokenExpiration,
241+
FederatedTokenFilePath: cfg.ServiceAccountFederatedTokenPath,
242+
TokenExpiration: cfg.ServiceAccountFederatedTokenExpiration,
243+
FederatedToken: cfg.ServiceAccountFederatedToken,
243244
}
244245

245246
if cfg.HTTPClient != nil && cfg.HTTPClient.Transport != nil {

core/clients/workload_identity_flow.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type WorkloadIdentityFederationFlow struct {
5959
type WorkloadIdentityFederationFlowConfig struct {
6060
TokenUrl string
6161
ClientID string
62+
FederatedToken string // Static token string. This is optional, if not set the token will be read from file.
6263
FederatedTokenFilePath string
6364
TokenExpiration string // Not supported yet
6465
BackgroundTokenRefreshContext context.Context // Functionality is enabled if this isn't nil
@@ -139,7 +140,7 @@ func (c *WorkloadIdentityFederationFlow) Init(cfg *WorkloadIdentityFederationFlo
139140
c.config.ClientID = getEnvOrDefault(clientIDEnv, "")
140141
}
141142

142-
if c.config.FederatedTokenFilePath == "" {
143+
if c.config.FederatedToken == "" && c.config.FederatedTokenFilePath == "" {
143144
c.config.FederatedTokenFilePath = getEnvOrDefault(FederatedTokenFileEnv, defaultFederatedTokenPath)
144145
}
145146

@@ -161,12 +162,6 @@ func (c *WorkloadIdentityFederationFlow) Init(cfg *WorkloadIdentityFederationFlo
161162
return err
162163
}
163164

164-
// // Init the token
165-
// _, err = c.GetAccessToken()
166-
// if err != nil {
167-
// return err
168-
// }
169-
170165
if c.config.BackgroundTokenRefreshContext != nil {
171166
go continuousRefreshToken(c)
172167
}
@@ -181,8 +176,10 @@ func (c *WorkloadIdentityFederationFlow) validate() error {
181176
if c.config.TokenUrl == "" {
182177
return fmt.Errorf("token URL cannot be empty")
183178
}
184-
if _, err := c.readJWTFromFileSystem(c.config.FederatedTokenFilePath); err != nil {
185-
return fmt.Errorf("error reading federated token file - %w", err)
179+
if c.config.FederatedToken == "" {
180+
if _, err := c.readJWTFromFileSystem(c.config.FederatedTokenFilePath); err != nil {
181+
return fmt.Errorf("error reading federated token file - %w", err)
182+
}
186183
}
187184
if c.tokenExpirationLeeway < 0 {
188185
return fmt.Errorf("token expiration leeway cannot be negative")
@@ -192,10 +189,14 @@ func (c *WorkloadIdentityFederationFlow) validate() error {
192189
}
193190

194191
// createAccessToken creates an access token using self signed JWT
195-
func (c *WorkloadIdentityFederationFlow) createAccessToken() (err error) {
196-
clientAssertion, err := c.readJWTFromFileSystem(c.config.FederatedTokenFilePath)
197-
if err != nil {
198-
return fmt.Errorf("error reading service account assertion - %w", err)
192+
func (c *WorkloadIdentityFederationFlow) createAccessToken() error {
193+
clientAssertion := c.config.FederatedToken
194+
if clientAssertion == "" {
195+
var err error
196+
clientAssertion, err = c.readJWTFromFileSystem(c.config.FederatedTokenFilePath)
197+
if err != nil {
198+
return fmt.Errorf("error reading service account assertion - %w", err)
199+
}
199200
}
200201

201202
res, err := c.requestToken(c.config.ClientID, clientAssertion)

0 commit comments

Comments
 (0)