Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AUTHENTICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,5 @@ Using this flow is less secure since the token is long-lived. You can provide th
# Optional: provide a file path containing the OIDC token
STACKIT_FEDERATED_TOKEN_FILE=/path/to/oidc-token.jwt
```

When running in Azure DevOps with the [STACKIT Service Connection](https://marketplace.visualstudio.com/items?itemName=SchwarzDigits.stackit-service-connection), the CLI reads `STACKIT_SERVICE_CONNECTION_ID` and requests the OIDC token for that service connection.
6 changes: 4 additions & 2 deletions internal/pkg/auth/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
EnvGitHubRequestToken = "ACTIONS_ID_TOKEN_REQUEST_TOKEN" //nolint:gosec // linter false positive
EnvAzureOIDCRequestURI = "SYSTEM_OIDCREQUESTURI"
EnvAzureAccessToken = "SYSTEM_ACCESSTOKEN" //nolint:gosec // linter false positive
EnvAzureServiceConnectionID = "STACKIT_SERVICE_CONNECTION_ID"
)

func IsOIDCEnabled() bool {
Expand All @@ -40,7 +41,8 @@ func OIDCServiceAccountEmail() string {
// TokenFunc returns the OIDCTokenFunc to use for Workload Identity Federation.
// It checks the following token sources in order: STACKIT_SERVICE_ACCOUNT_FEDERATED_TOKEN,
// STACKIT_FEDERATED_TOKEN_FILE, GitHub Actions (ACTIONS_ID_TOKEN_REQUEST_URL +
// ACTIONS_ID_TOKEN_REQUEST_TOKEN), and Azure DevOps (SYSTEM_OIDCREQUESTURI + SYSTEM_ACCESSTOKEN).
// ACTIONS_ID_TOKEN_REQUEST_TOKEN), and Azure DevOps (SYSTEM_OIDCREQUESTURI + SYSTEM_ACCESSTOKEN,
// optionally scoped to the service connection set in STACKIT_SERVICE_CONNECTION_ID).
// Returns an error if no source is detected.
func OIDCTokenFunc() (oidcadapters.OIDCTokenFunc, error) {
// static token provided directly via env var
Expand All @@ -65,7 +67,7 @@ func OIDCTokenFunc() (oidcadapters.OIDCTokenFunc, error) {
// Azure DevOps
if adoURL := os.Getenv(EnvAzureOIDCRequestURI); adoURL != "" {
if adoToken := os.Getenv(EnvAzureAccessToken); adoToken != "" {
return oidcadapters.RequestAzureDevOpsOIDCToken(adoURL, adoToken, ""), nil
return oidcadapters.RequestAzureDevOpsOIDCToken(adoURL, adoToken, os.Getenv(EnvAzureServiceConnectionID)), nil
}
}

Expand Down
57 changes: 57 additions & 0 deletions internal/pkg/auth/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
Expand Down Expand Up @@ -169,6 +170,62 @@ func TestTokenFunc_AzureDevOps(t *testing.T) {
}
}

func TestTokenFunc_AzureDevOps_ServiceConnectionID(t *testing.T) {
tests := []struct {
description string
serviceConnectionID string
}{
{
description: "service connection id set",
serviceConnectionID: "my-service-connection-id",
},
{
description: "service connection id unset",
serviceConnectionID: "",
},
}

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
var gotQuery url.Values
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotQuery = r.URL.Query()
w.Header().Set("Content-Type", "application/json")
tok := "ado-oidc-token"
_ = json.NewEncoder(w).Encode(map[string]*string{"oidcToken": &tok})
}))
defer srv.Close()

t.Setenv(auth.EnvServiceAccountFederatedToken, "")
t.Setenv(auth.EnvFederatedTokenFile, "")
t.Setenv(auth.EnvGitHubRequestURL, "")
t.Setenv(auth.EnvGitHubRequestToken, "")
t.Setenv(auth.EnvAzureOIDCRequestURI, srv.URL)
t.Setenv(auth.EnvAzureAccessToken, "ado-access-token")
t.Setenv(auth.EnvAzureServiceConnectionID, tt.serviceConnectionID)

fn, err := auth.OIDCTokenFunc()
if err != nil {
t.Fatalf("OIDCTokenFunc() unexpected error: %v", err)
}
if _, err := fn(context.Background()); err != nil {
t.Fatalf("fn() unexpected error: %v", err)
}

got, ok := gotQuery["serviceConnectionId"]
if tt.serviceConnectionID == "" {
if ok {
t.Errorf("serviceConnectionId query param = %q, want it absent", got)
}
return
}
if !ok || len(got) != 1 || got[0] != tt.serviceConnectionID {
t.Errorf("serviceConnectionId query param = %q, want %q", got, tt.serviceConnectionID)
}
})
}
}

func TestTokenFunc_NoSource(t *testing.T) {
// All env vars absent → must return an actionable error, no panic.
t.Setenv(auth.EnvServiceAccountFederatedToken, "")
Expand Down
Loading