-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ashutosh Kumar <[email protected]>
- Loading branch information
1 parent
1006ee7
commit 0b23c7d
Showing
9 changed files
with
131 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package scope | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azidentity" | ||
) | ||
|
||
type workloadIdentityCredential struct { | ||
assertion string | ||
file string | ||
cred *azidentity.ClientAssertionCredential | ||
lastRead time.Time | ||
} | ||
|
||
type WorkloadIdentityCredentialOptions struct { | ||
ClientID string | ||
TenantID string | ||
TokenFilePath string | ||
azcore.ClientOptions | ||
} | ||
|
||
func NewWorkloadIdentityCredentialOptions() *WorkloadIdentityCredentialOptions { | ||
return &WorkloadIdentityCredentialOptions{} | ||
} | ||
|
||
func (w *WorkloadIdentityCredentialOptions) WithClientID(clientID string) *WorkloadIdentityCredentialOptions { | ||
w.ClientID = clientID | ||
return w | ||
} | ||
|
||
func (w *WorkloadIdentityCredentialOptions) WithTenantID(tenantID string) *WorkloadIdentityCredentialOptions { | ||
w.TenantID = tenantID | ||
return w | ||
} | ||
|
||
func (w *WorkloadIdentityCredentialOptions) WithTokenFilePath(tokenFilePath string) *WorkloadIdentityCredentialOptions { | ||
w.TokenFilePath = tokenFilePath | ||
return w | ||
} | ||
|
||
func NewWorkloadIdentityCredential(options *WorkloadIdentityCredentialOptions) (*workloadIdentityCredential, error) { | ||
w := &workloadIdentityCredential{file: options.TokenFilePath} | ||
cred, err := azidentity.NewClientAssertionCredential(options.TenantID, options.ClientID, w.getAssertion, &azidentity.ClientAssertionCredentialOptions{ClientOptions: options.ClientOptions}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
w.cred = cred | ||
return w, nil | ||
} | ||
|
||
func (w *workloadIdentityCredential) GetToken(ctx context.Context, opts policy.TokenRequestOptions) (azcore.AccessToken, error) { | ||
return w.cred.GetToken(ctx, opts) | ||
} | ||
|
||
func (w *workloadIdentityCredential) getAssertion(context.Context) (string, error) { | ||
if now := time.Now(); w.lastRead.Add(5 * time.Minute).Before(now) { | ||
content, err := os.ReadFile(w.file) | ||
if err != nil { | ||
return "", err | ||
} | ||
w.assertion = string(content) | ||
w.lastRead = now | ||
} | ||
return w.assertion, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
labels: | ||
azure.workload.identity/use: "true" | ||
name: manager | ||
namespace: system | ||
namespace: system |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters