forked from jfrog/vault-plugin-secrets-artifactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttl_test.go
115 lines (94 loc) · 3 KB
/
ttl_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
package artifactory
import (
"context"
"testing"
"time"
"github.com/hashicorp/vault/sdk/logical"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
// I've centralized all the tests involving the interplay of TTLs.
// Role with no Max TTL must use the system max TTL when creating tokens.
func TestBackend_NoRoleMaxTTLUsesSystemMaxTTL(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockArtifactoryUsageVersionRequests("")
httpmock.RegisterResponder(
"POST",
"http://myserver.com:80/artifactory/api/security/token",
httpmock.NewStringResponder(200, `
{
"access_token": "adsdgbtybbeeyh...",
"expires_in": 0,
"scope": "api:* member-of-groups:readers",
"token_type": "Bearer",
"refresh_token": "fgsfgsdugh8dgu9s8gy9hsg..."
}
`))
b, config := configuredBackend(t, map[string]interface{}{
"access_token": "test-access-token",
"url": "http://myserver.com:80/artifactory",
})
// Role with no maximum TTL
roleData := map[string]interface{}{
"role": "test-role",
"username": "test-username",
"scope": "test-scope",
}
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "roles/test-role",
Storage: config.StorageView,
Data: roleData,
})
assert.Nil(t, resp)
assert.NoError(t, err)
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "token/test-role",
Storage: config.StorageView,
})
assert.NotNil(t, resp)
assert.NoError(t, err)
assert.EqualValues(t, config.System.MaxLeaseTTL(), resp.Secret.MaxTTL)
}
// With a role max_ttl not greater than the system max_ttl, the max_ttl for a token must
// be the role max_ttl.
func TestBackend_WorkingWithBothMaxTTLs(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockArtifactoryUsageVersionRequests("")
httpmock.RegisterResponder(
"POST",
"http://myserver.com:80/artifactory/api/security/token",
httpmock.NewStringResponder(200, canonicalAccessToken))
b, config := configuredBackend(t, map[string]interface{}{
"access_token": "test-access-token",
"url": "http://myserver.com:80/artifactory",
"max_ttl": 10 * time.Minute,
})
// Role with no maximum TTL
roleData := map[string]interface{}{
"role": "test-role",
"username": "test-username",
"scope": "test-scope",
"max_ttl": 9 * time.Minute,
}
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "roles/test-role",
Storage: config.StorageView,
Data: roleData,
})
assert.Nil(t, resp)
assert.NoError(t, err)
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "token/test-role",
Storage: config.StorageView,
})
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.False(t, resp.IsError())
assert.EqualValues(t, 9*time.Minute, resp.Secret.MaxTTL)
}