Skip to content

Commit 633d33f

Browse files
committed
Add PAT (Personal Access Token) auth support (CRED-2147)
Add DD_PAT environment variable support to NewDefaultContext(). When set, the PAT value is used as the appKeyAuth key (sent as DD-APPLICATION-KEY header), taking precedence over DD_APP_KEY when both are set. Changes: - api/datadog/configuration.go: Add DD_PAT env var handling after DD_APP_KEY - .generator/src/generator/templates/configuration.j2: Same change in template - tests/api/client_test.go: Add 4 unit tests for PAT auth scenarios
1 parent 57ebae9 commit 633d33f

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

.generator/src/generator/templates/configuration.j2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ func NewDefaultContext(ctx context.Context) context.Context {
400400
}
401401

402402
{%- endfor %}
403+
if pat, ok := os.LookupEnv("DD_PAT"); ok {
404+
keys["appKeyAuth"] = APIKey{Key: pat}
405+
}
403406
ctx = context.WithValue(
404407
ctx,
405408
ContextAPIKeys,

api/datadog/configuration.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,9 @@ func NewDefaultContext(ctx context.Context) context.Context {
10641064
if apiKey, ok := os.LookupEnv("DD_APP_KEY"); ok {
10651065
keys["appKeyAuth"] = APIKey{Key: apiKey}
10661066
}
1067+
if pat, ok := os.LookupEnv("DD_PAT"); ok {
1068+
keys["appKeyAuth"] = APIKey{Key: pat}
1069+
}
10671070
ctx = context.WithValue(
10681071
ctx,
10691072
ContextAPIKeys,

tests/api/client_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,56 @@ func TestApiAppKeyAuthenticate(t *testing.T) {
155155
assert.Equal(t, headers["DD-API-KEY"], apiKey)
156156
assert.Equal(t, headers["DD-APPLICATION-KEY"], appKey)
157157
}
158+
159+
func TestPATAuthentication(t *testing.T) {
160+
apiKey := "api-key"
161+
pat := "ddapp_test-pat-token"
162+
keys := map[string]datadog.APIKey{
163+
"apiKeyAuth": {Key: apiKey},
164+
"appKeyAuth": {Key: pat},
165+
}
166+
testAuthCtx := context.WithValue(
167+
context.Background(),
168+
datadog.ContextAPIKeys,
169+
keys,
170+
)
171+
172+
headers := map[string]string{}
173+
datadog.SetAuthKeys(
174+
testAuthCtx,
175+
&headers,
176+
[2]string{"apiKeyAuth", "DD-API-KEY"},
177+
[2]string{"appKeyAuth", "DD-APPLICATION-KEY"},
178+
)
179+
assert.Equal(t, apiKey, headers["DD-API-KEY"])
180+
assert.Equal(t, pat, headers["DD-APPLICATION-KEY"])
181+
}
182+
183+
func TestNewDefaultContextWithPAT(t *testing.T) {
184+
t.Setenv("DD_API_KEY", "test-api-key")
185+
t.Setenv("DD_PAT", "ddapp_test-pat-token")
186+
187+
ctx := datadog.NewDefaultContext(context.Background())
188+
keys := ctx.Value(datadog.ContextAPIKeys).(map[string]datadog.APIKey)
189+
assert.Equal(t, "test-api-key", keys["apiKeyAuth"].Key)
190+
assert.Equal(t, "ddapp_test-pat-token", keys["appKeyAuth"].Key)
191+
}
192+
193+
func TestNewDefaultContextPATOverridesAppKey(t *testing.T) {
194+
t.Setenv("DD_API_KEY", "test-api-key")
195+
t.Setenv("DD_APP_KEY", "test-app-key")
196+
t.Setenv("DD_PAT", "ddapp_test-pat-token")
197+
198+
ctx := datadog.NewDefaultContext(context.Background())
199+
keys := ctx.Value(datadog.ContextAPIKeys).(map[string]datadog.APIKey)
200+
assert.Equal(t, "ddapp_test-pat-token", keys["appKeyAuth"].Key)
201+
}
202+
203+
func TestNewDefaultContextAppKeyWithoutPAT(t *testing.T) {
204+
t.Setenv("DD_API_KEY", "test-api-key")
205+
t.Setenv("DD_APP_KEY", "test-app-key")
206+
207+
ctx := datadog.NewDefaultContext(context.Background())
208+
keys := ctx.Value(datadog.ContextAPIKeys).(map[string]datadog.APIKey)
209+
assert.Equal(t, "test-app-key", keys["appKeyAuth"].Key)
210+
}

0 commit comments

Comments
 (0)