-
Notifications
You must be signed in to change notification settings - Fork 102
/
admin_setting_oidc_integration_test.go
137 lines (100 loc) · 2.85 KB
/
admin_setting_oidc_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type wellKnownJwks struct {
Keys []struct {
Kid string `json:"kid"`
} `json:"keys"`
}
func TestAdminSettings_Oidc_RotateKey(t *testing.T) {
skipUnlessEnterprise(t)
client := testClient(t)
jwksClient := http.Client{
Timeout: time.Second * 2,
}
baseURL := client.baseURL
token := client.token
ctx := context.Background()
jwks, err := getJwks(jwksClient, baseURL, token)
require.NoError(t, err)
// Don't assume there is only 1 key to start
originalNumKeys := len(jwks.Keys)
err = client.Admin.Settings.OIDC.RotateKey(ctx)
require.NoError(t, err)
jwks, err = getJwks(jwksClient, baseURL, token)
require.NoError(t, err)
newNumKeys := len(jwks.Keys)
// Rotate should add 1 additional key
assert.Equal(t, originalNumKeys+1, newNumKeys)
}
func TestAdminSettings_Oidc_TrimKey(t *testing.T) {
skipUnlessEnterprise(t)
client := testClient(t)
jwksClient := http.Client{
Timeout: time.Second * 2,
}
baseURL := client.baseURL
token := client.token
ctx := context.Background()
jwks, err := getJwks(jwksClient, baseURL, token)
require.NoError(t, err)
// Don't assume there is only 1 key to start
originalNumKeys := len(jwks.Keys)
originalKids := make([]string, originalNumKeys)
for i := 0; i < originalNumKeys; i++ {
originalKids[i] = jwks.Keys[i].Kid
}
err = client.Admin.Settings.OIDC.RotateKey(ctx)
require.NoError(t, err)
jwks, err = getJwks(jwksClient, baseURL, token)
require.NoError(t, err)
beforeTrimNumKeys := len(jwks.Keys)
assert.Equal(t, originalNumKeys+1, beforeTrimNumKeys)
err = client.Admin.Settings.OIDC.TrimKey(ctx)
require.NoError(t, err)
jwks, err = getJwks(jwksClient, baseURL, token)
require.NoError(t, err)
afterTrimNumKeys := len(jwks.Keys)
assert.Equal(t, 1, afterTrimNumKeys)
// Make sure we actually trimmed the keys
assert.NotContains(t, originalKids, jwks.Keys[0].Kid)
}
func getJwks(client http.Client, baseURL *url.URL, token string) (*wellKnownJwks, error) {
jwksEndpoint, err := baseURL.Parse("/.well-known/jwks")
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodGet, jwksEndpoint.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Accept", "application/json")
res, getErr := client.Do(req)
if getErr != nil {
return nil, getErr
}
if res.Body != nil {
defer res.Body.Close()
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d. Expected a 200 response", res.StatusCode)
}
var result wellKnownJwks
jsonErr := json.NewDecoder(res.Body).Decode(&result)
if jsonErr != nil {
return nil, jsonErr
}
return &result, nil
}