-
Notifications
You must be signed in to change notification settings - Fork 3
/
keycloak.go
235 lines (206 loc) · 7.22 KB
/
keycloak.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package keycloak
import (
"context"
"fmt"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"path/filepath"
)
const (
defaultRealmImport = "/opt/keycloak/data/import/"
defaultProviders = "/opt/keycloak/providers/"
tlsFilePath = "/opt/keycloak/conf"
defaultKeycloakAdminUsername = "admin"
defaultKeycloakAdminPassword = "admin"
defaultKeycloakContextPath = "/"
keycloakAdminUsernameEnv = "KEYCLOAK_ADMIN"
keycloakAdminPasswordEnv = "KEYCLOAK_ADMIN_PASSWORD"
keycloakContextPathEnv = "KEYCLOAK_CONTEXT_PATH"
keycloakTlsEnv = "KEYCLOAK_TLS"
keycloakStartupCommand = "start-dev"
keycloakPort = "8080/tcp"
keycloakHttpsPort = "8443/tcp"
)
// KeycloakContainer is a wrapper around testcontainers.Container
// that provides some convenience methods for working with Keycloak.
type KeycloakContainer struct {
testcontainers.Container
username string
password string
enableTLS bool
contextPath string
}
// GetAdminClient returns an AdminClient for the KeycloakContainer.
func (k *KeycloakContainer) GetAdminClient(ctx context.Context) (*AdminClient, error) {
authServerURL, err := k.GetAuthServerURL(ctx)
if err != nil {
return nil, err
}
return NewAdminClient(&ctx, authServerURL, k.username, k.password)
}
// GetAuthServerURL returns the URL of the KeycloakContainer.
func (k *KeycloakContainer) GetAuthServerURL(ctx context.Context) (string, error) {
host, err := k.Host(ctx)
if err != nil {
return "", err
}
if k.enableTLS {
port, err := k.MappedPort(ctx, keycloakHttpsPort)
if err != nil {
return "", err
}
return fmt.Sprintf("https://%s:%s%s", host, port.Port(), k.contextPath), nil
} else {
port, err := k.MappedPort(ctx, keycloakPort)
if err != nil {
return "", err
}
return fmt.Sprintf("http://%s:%s%s", host, port.Port(), k.contextPath), nil
}
}
// Run starts a new KeycloakContainer with the given options.
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*KeycloakContainer, error) {
req := testcontainers.ContainerRequest{
Image: img,
ImageSubstitutors: []testcontainers.ImageSubstitutor{
testcontainers.NewCustomHubSubstitutor("quay.io"),
},
Env: map[string]string{
keycloakAdminUsernameEnv: defaultKeycloakAdminUsername,
keycloakAdminPasswordEnv: defaultKeycloakAdminPassword,
},
ExposedPorts: []string{keycloakPort},
Cmd: []string{keycloakStartupCommand},
}
genericContainerReq := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
}
for _, opt := range opts {
if err := opt.Customize(&genericContainerReq); err != nil {
return nil, err
}
}
if genericContainerReq.WaitingFor == nil {
contextPath := genericContainerReq.Env[keycloakContextPathEnv]
if contextPath == "" {
contextPath = defaultKeycloakContextPath
}
if genericContainerReq.Env[keycloakTlsEnv] != "" {
genericContainerReq.WaitingFor = wait.ForAll(wait.ForHTTP(contextPath).
WithPort(keycloakHttpsPort).
WithTLS(true).
WithAllowInsecure(true),
wait.ForLog("Running the server"))
} else {
genericContainerReq.WaitingFor = wait.ForAll(wait.ForHTTP(contextPath),
wait.ForLog("Running the server"))
}
}
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
if err != nil {
return nil, err
}
return &KeycloakContainer{
Container: container,
username: genericContainerReq.Env[keycloakAdminUsernameEnv],
password: genericContainerReq.Env[keycloakAdminPasswordEnv],
contextPath: genericContainerReq.Env[keycloakContextPathEnv],
enableTLS: genericContainerReq.Env[keycloakTlsEnv] != "",
}, nil
}
// WithRealmImportFile is option to import a realm file into KeycloakContainer.
func WithRealmImportFile(realmImportFile string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
realmFile := testcontainers.ContainerFile{
HostFilePath: realmImportFile,
ContainerFilePath: defaultRealmImport + filepath.Base(realmImportFile),
FileMode: 0o755,
}
req.Files = append(req.Files, realmFile)
processKeycloakArgs(req, []string{"--import-realm"})
return nil
}
}
// WithProviders is option to set the providers for KeycloakContainer.
// Providers should be packaged ina Java Archive (JAR) file.
// See https://www.keycloak.org/server/configuration-provider
func WithProviders(providerFiles ...string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
for _, providerFile := range providerFiles {
provider := testcontainers.ContainerFile{
HostFilePath: providerFile,
ContainerFilePath: defaultProviders + filepath.Base(providerFile),
FileMode: 0o755,
}
req.Files = append(req.Files, provider)
}
return nil
}
}
// WithTLS is option to enable TLS for KeycloakContainer.
func WithTLS(certFile, keyFile string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.ExposedPorts = []string{keycloakHttpsPort}
cf := testcontainers.ContainerFile{
HostFilePath: certFile,
ContainerFilePath: tlsFilePath + "/tls.crt",
FileMode: 0o755,
}
kf := testcontainers.ContainerFile{
HostFilePath: keyFile,
ContainerFilePath: tlsFilePath + "/tls.key",
FileMode: 0o755,
}
req.Files = append(req.Files, cf, kf)
req.Env[keycloakTlsEnv] = "true"
processKeycloakArgs(req,
[]string{"--https-certificate-file=" + tlsFilePath + "/tls.crt",
"--https-certificate-key-file=" + tlsFilePath + "/tls.key"},
)
return nil
}
}
// WithAdminUsername is option to set the admin username for KeycloakContainer.
func WithAdminUsername(username string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
if username == "" {
username = defaultKeycloakAdminUsername
}
req.Env[keycloakAdminUsernameEnv] = username
return nil
}
}
// WithAdminPassword is option to set the admin password for KeycloakContainer.
func WithAdminPassword(password string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
if password == "" {
password = defaultKeycloakAdminPassword
}
req.Env[keycloakAdminPasswordEnv] = password
return nil
}
}
// WithContextPath is option to set the context path for KeycloakContainer.
func WithContextPath(contextPath string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
if contextPath == "" {
contextPath = defaultKeycloakContextPath
}
req.Env[keycloakContextPathEnv] = contextPath
processKeycloakArgs(req, []string{"--http-relative-path=" + contextPath})
return nil
}
}
func processKeycloakArgs(req *testcontainers.GenericContainerRequest, args []string) {
if len(req.Cmd) == 0 {
req.Cmd = append([]string{keycloakStartupCommand}, args...)
return
}
if req.Cmd[0] == keycloakStartupCommand {
req.Cmd = append(req.Cmd, args...)
} else if req.Cmd[0] != keycloakStartupCommand {
req.Cmd = append([]string{keycloakStartupCommand}, req.Cmd...)
req.Cmd = append(req.Cmd, args...)
}
}