-
Notifications
You must be signed in to change notification settings - Fork 61
/
provider_ibmisam_test.go
147 lines (123 loc) · 3.45 KB
/
provider_ibmisam_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
138
139
140
141
142
143
144
145
146
147
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package jwtauth
import (
"bytes"
"context"
"encoding/pem"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
)
type ibmisamServer struct {
t *testing.T
server *httptest.Server
}
func newibmisamServer(t *testing.T) *ibmisamServer {
a := new(ibmisamServer)
a.t = t
a.server = httptest.NewTLSServer(a)
return a
}
func (a *ibmisamServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/.well-known/openid-configuration":
w.Write([]byte(strings.Replace(`
{
"issuer": "%s",
"authorization_endpoint": "%s/auth",
"token_endpoint": "%s/oauth2/v2.0/token",
"jwks_uri": "%s/certs",
"userinfo_endpoint": "%s/userinfo"
}`, "%s", a.server.URL, -1)))
default:
a.t.Fatalf("unexpected path: %q", r.URL.Path)
}
}
// getTLSCert returns the certificate for this provider in PEM format
func (a *ibmisamServer) getTLSCert() (string, error) {
cert := a.server.Certificate()
block := &pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Raw,
}
pemBuf := new(bytes.Buffer)
if err := pem.Encode(pemBuf, block); err != nil {
return "", err
}
return pemBuf.String(), nil
}
func TestLogin_ibmisam_fetchGroups(t *testing.T) {
aServer := newibmisamServer(t)
aCert, err := aServer.getTLSCert()
require.NoError(t, err)
b, storage := getBackend(t)
ctx := context.Background()
data := map[string]interface{}{
"oidc_discovery_url": aServer.server.URL,
"oidc_discovery_ca_pem": aCert,
"oidc_client_id": "abc",
"oidc_client_secret": "def",
"default_role": "test",
"bound_issuer": "http://vault.example.com/",
"provider_config": map[string]interface{}{
"provider": "ibmisam",
},
}
// basic configuration
req := &logical.Request{
Operation: logical.UpdateOperation,
Path: configPath,
Storage: storage,
Data: data,
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%v resp:%#v\n", err, resp)
}
// set up test role
data = map[string]interface{}{
"user_claim": "email",
"groups_claim": "groups",
"allowed_redirect_uris": []string{"https://example.com"},
}
req = &logical.Request{
Operation: logical.CreateOperation,
Path: "role/test",
Storage: storage,
Data: data,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%v resp:%#v\n", err, resp)
}
role := &jwtRole{
GroupsClaim: "groups",
}
allClaims := map[string]interface{}{
"groups": "a-group another-group",
}
// Ensure b.cachedConfig is populated
config, err := b.(*jwtAuthBackend).config(ctx, storage)
if err != nil {
t.Fatal(err)
}
// Initialize the ibmisam provider
provider, err := NewProviderConfig(ctx, config, ProviderMap())
if err != nil {
t.Fatal(err)
}
// Ensure groups are as expected
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "test.access.token"})
groupsRaw, err := b.(*jwtAuthBackend).fetchGroups(ctx, provider, allClaims, role, tokenSource)
assert.NoError(t, err)
groupsResp, ok := normalizeList(groupsRaw)
assert.True(t, ok)
assert.Equal(t, []interface{}{"a-group", "another-group"}, groupsResp)
}