Skip to content

Commit ab9269e

Browse files
authored
use keycloak's native organizations feature (#937)
1 parent 6e23e9a commit ab9269e

File tree

21 files changed

+3221
-180
lines changed

21 files changed

+3221
-180
lines changed

libs/hwauthz/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ require (
9797
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
9898
github.com/shoenig/go-m1cpu v0.1.6 // indirect
9999
github.com/sirupsen/logrus v1.9.3 // indirect
100-
github.com/testcontainers/testcontainers-go v0.34.0 // indirect
100+
github.com/stillya/testcontainers-keycloak v0.3.2 // indirect
101+
github.com/testcontainers/testcontainers-go v0.35.0 // indirect
101102
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0 // indirect
102103
github.com/testcontainers/testcontainers-go/modules/redis v0.34.0 // indirect
103104
github.com/tklauser/go-sysconf v0.3.12 // indirect

libs/hwauthz/go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnj
229229
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
230230
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
231231
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
232+
github.com/stillya/testcontainers-keycloak v0.3.2 h1:6CEtUW2IX8O+UIoohyvn4V129ErXd2WqKEhCYEsKxpQ=
233+
github.com/stillya/testcontainers-keycloak v0.3.2/go.mod h1:jVkcb6gxRSlulwe76yv3MlBvqVzXhJO4W/4kF1covOc=
232234
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
233235
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
234236
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
@@ -245,8 +247,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
245247
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
246248
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
247249
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
248-
github.com/testcontainers/testcontainers-go v0.34.0 h1:5fbgF0vIN5u+nD3IWabQwRybuB4GY8G2HHgCkbMzMHo=
249-
github.com/testcontainers/testcontainers-go v0.34.0/go.mod h1:6P/kMkQe8yqPHfPWNulFGdFHTD8HB2vLq/231xY2iPQ=
250+
github.com/testcontainers/testcontainers-go v0.35.0 h1:uADsZpTKFAtp8SLK+hMwSaa+X+JiERHtd4sQAFmXeMo=
251+
github.com/testcontainers/testcontainers-go v0.35.0/go.mod h1:oEVBj5zrfJTrgjwONs1SsRbnBtH9OKl+IGl3UMcr2B4=
250252
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0 h1:c51aBXT3v2HEBVarmaBnsKzvgZjC5amn0qsj8Naqi50=
251253
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0/go.mod h1:EWP75ogLQU4M4L8U+20mFipjV4WIR9WtlMXSB6/wiuc=
252254
github.com/testcontainers/testcontainers-go/modules/redis v0.34.0 h1:HkkKZPi6W2I+ywqplvnKOYRBKXQgpdxErBbdgx8F8nw=

libs/hwtesting/containers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717
Eventstore ContainerOpt = iota
1818
Redis ContainerOpt = iota
1919
Spice ContainerOpt = iota
20+
Keycloak ContainerOpt = iota
2021
)
2122

2223
type Endpoints []string
@@ -51,6 +52,8 @@ func StartContainers(ctx context.Context, opts ...ContainerOpt) (endpoints Endpo
5152
f = startRedis
5253
case Spice:
5354
f = startSpiceDB
55+
case Keycloak:
56+
f = startKeycloak
5457
default:
5558
panic("unknown ContainerOpt provided")
5659
}

libs/hwtesting/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ require (
1717
github.com/authzed/grpcutil v0.0.0-20240123194739-2ea1e3d2d98b
1818
github.com/golang-migrate/migrate/v4 v4.18.1
1919
github.com/rs/zerolog v1.33.0
20-
github.com/testcontainers/testcontainers-go v0.34.0
20+
github.com/stillya/testcontainers-keycloak v0.3.2
21+
github.com/testcontainers/testcontainers-go v0.35.0
2122
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0
2223
github.com/testcontainers/testcontainers-go/modules/redis v0.34.0
2324
google.golang.org/grpc v1.69.2

libs/hwtesting/go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnj
229229
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
230230
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
231231
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
232+
github.com/stillya/testcontainers-keycloak v0.3.2 h1:6CEtUW2IX8O+UIoohyvn4V129ErXd2WqKEhCYEsKxpQ=
233+
github.com/stillya/testcontainers-keycloak v0.3.2/go.mod h1:jVkcb6gxRSlulwe76yv3MlBvqVzXhJO4W/4kF1covOc=
232234
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
233235
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
234236
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
@@ -245,8 +247,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
245247
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
246248
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
247249
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
248-
github.com/testcontainers/testcontainers-go v0.34.0 h1:5fbgF0vIN5u+nD3IWabQwRybuB4GY8G2HHgCkbMzMHo=
249-
github.com/testcontainers/testcontainers-go v0.34.0/go.mod h1:6P/kMkQe8yqPHfPWNulFGdFHTD8HB2vLq/231xY2iPQ=
250+
github.com/testcontainers/testcontainers-go v0.35.0 h1:uADsZpTKFAtp8SLK+hMwSaa+X+JiERHtd4sQAFmXeMo=
251+
github.com/testcontainers/testcontainers-go v0.35.0/go.mod h1:oEVBj5zrfJTrgjwONs1SsRbnBtH9OKl+IGl3UMcr2B4=
250252
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0 h1:c51aBXT3v2HEBVarmaBnsKzvgZjC5amn0qsj8Naqi50=
251253
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0/go.mod h1:EWP75ogLQU4M4L8U+20mFipjV4WIR9WtlMXSB6/wiuc=
252254
github.com/testcontainers/testcontainers-go/modules/redis v0.34.0 h1:HkkKZPi6W2I+ywqplvnKOYRBKXQgpdxErBbdgx8F8nw=

libs/hwtesting/keycloak.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package hwtesting
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
zlog "github.com/rs/zerolog/log"
9+
"github.com/stillya/testcontainers-keycloak"
10+
)
11+
12+
const ImageKeycloak = "quay.io/keycloak/keycloak:26.1.4"
13+
14+
const (
15+
KeycloakAdminUser = "admin"
16+
KeycloakAdminPassword = "admin"
17+
)
18+
19+
// realm.json
20+
21+
const (
22+
KeycloakClientID = "user-svc"
23+
KeycloakRealm = "main"
24+
KeycloakClientSecret = "test-secret"
25+
)
26+
27+
type realmImportFilePathKey struct{}
28+
29+
func WithKeycloakRealmImportFile(ctx context.Context, path string) context.Context {
30+
return context.WithValue(ctx, realmImportFilePathKey{}, path)
31+
}
32+
33+
func getKeycloakRealmImportFile(ctx context.Context) string {
34+
path, ok := ctx.Value(realmImportFilePathKey{}).(string)
35+
if !ok {
36+
zlog.Fatal().Msg("Keycloak container started, but WithKeycloakRealmImportFile() not used")
37+
}
38+
return path
39+
}
40+
41+
func startKeycloak(ctx context.Context) (endpoint string, teardown func()) {
42+
container, err := keycloak.Run(ctx,
43+
ImageKeycloak,
44+
keycloak.WithAdminUsername(KeycloakAdminUser),
45+
keycloak.WithAdminPassword(KeycloakAdminPassword),
46+
keycloak.WithRealmImportFile(getKeycloakRealmImportFile(ctx)),
47+
)
48+
if err != nil {
49+
zlog.Fatal().Err(err).Msg("Could not start keycloak")
50+
}
51+
endpoint, err = container.Endpoint(ctx, "")
52+
if err != nil {
53+
zlog.Fatal().Err(err).Msg("Could not get access to keycloak endpoint")
54+
}
55+
return endpoint, func() {
56+
if err := container.Terminate(ctx); err != nil {
57+
zlog.Fatal().Err(err).Msg("Could not terminate keycloak container")
58+
}
59+
}
60+
}
61+
62+
func SetKeycloakEnv(endpoint string) {
63+
m := map[string]string{
64+
"OAUTH_ISSUER_URL": issURL(endpoint),
65+
"OAUTH_CLIENT_ID": KeycloakClientID,
66+
"OAUTH_KC_CLIENT_SECRET": KeycloakClientSecret,
67+
"OAUTH_KC_REALM": KeycloakRealm,
68+
}
69+
for k, v := range m {
70+
if err := os.Setenv(k, v); err != nil {
71+
zlog.Fatal().Err(err).Msgf("Could not set %s=%s", k, v)
72+
}
73+
}
74+
}
75+
76+
func issURL(endpoint string) string {
77+
return fmt.Sprintf("http://%s/realms/%s", endpoint, KeycloakRealm)
78+
}

services/property-svc/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ require (
112112
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
113113
github.com/shoenig/go-m1cpu v0.1.6 // indirect
114114
github.com/sirupsen/logrus v1.9.3 // indirect
115-
github.com/testcontainers/testcontainers-go v0.34.0 // indirect
115+
github.com/stillya/testcontainers-keycloak v0.3.2 // indirect
116+
github.com/testcontainers/testcontainers-go v0.35.0 // indirect
116117
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0 // indirect
117118
github.com/testcontainers/testcontainers-go/modules/redis v0.34.0 // indirect
118119
github.com/tklauser/go-sysconf v0.3.12 // indirect

services/property-svc/go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnj
237237
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
238238
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
239239
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
240+
github.com/stillya/testcontainers-keycloak v0.3.2 h1:6CEtUW2IX8O+UIoohyvn4V129ErXd2WqKEhCYEsKxpQ=
241+
github.com/stillya/testcontainers-keycloak v0.3.2/go.mod h1:jVkcb6gxRSlulwe76yv3MlBvqVzXhJO4W/4kF1covOc=
240242
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
241243
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
242244
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
@@ -253,8 +255,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
253255
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
254256
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
255257
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
256-
github.com/testcontainers/testcontainers-go v0.34.0 h1:5fbgF0vIN5u+nD3IWabQwRybuB4GY8G2HHgCkbMzMHo=
257-
github.com/testcontainers/testcontainers-go v0.34.0/go.mod h1:6P/kMkQe8yqPHfPWNulFGdFHTD8HB2vLq/231xY2iPQ=
258+
github.com/testcontainers/testcontainers-go v0.35.0 h1:uADsZpTKFAtp8SLK+hMwSaa+X+JiERHtd4sQAFmXeMo=
259+
github.com/testcontainers/testcontainers-go v0.35.0/go.mod h1:oEVBj5zrfJTrgjwONs1SsRbnBtH9OKl+IGl3UMcr2B4=
258260
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0 h1:c51aBXT3v2HEBVarmaBnsKzvgZjC5amn0qsj8Naqi50=
259261
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0/go.mod h1:EWP75ogLQU4M4L8U+20mFipjV4WIR9WtlMXSB6/wiuc=
260262
github.com/testcontainers/testcontainers-go/modules/redis v0.34.0 h1:HkkKZPi6W2I+ywqplvnKOYRBKXQgpdxErBbdgx8F8nw=

services/tasks-svc/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ require (
118118
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
119119
github.com/shoenig/go-m1cpu v0.1.6 // indirect
120120
github.com/sirupsen/logrus v1.9.3 // indirect
121-
github.com/testcontainers/testcontainers-go v0.34.0 // indirect
121+
github.com/stillya/testcontainers-keycloak v0.3.2 // indirect
122+
github.com/testcontainers/testcontainers-go v0.35.0 // indirect
122123
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0 // indirect
123124
github.com/testcontainers/testcontainers-go/modules/redis v0.34.0 // indirect
124125
github.com/tklauser/go-sysconf v0.3.12 // indirect

services/tasks-svc/go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnj
251251
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
252252
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
253253
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
254+
github.com/stillya/testcontainers-keycloak v0.3.2 h1:6CEtUW2IX8O+UIoohyvn4V129ErXd2WqKEhCYEsKxpQ=
255+
github.com/stillya/testcontainers-keycloak v0.3.2/go.mod h1:jVkcb6gxRSlulwe76yv3MlBvqVzXhJO4W/4kF1covOc=
254256
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
255257
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
256258
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
@@ -267,8 +269,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
267269
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
268270
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
269271
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
270-
github.com/testcontainers/testcontainers-go v0.34.0 h1:5fbgF0vIN5u+nD3IWabQwRybuB4GY8G2HHgCkbMzMHo=
271-
github.com/testcontainers/testcontainers-go v0.34.0/go.mod h1:6P/kMkQe8yqPHfPWNulFGdFHTD8HB2vLq/231xY2iPQ=
272+
github.com/testcontainers/testcontainers-go v0.35.0 h1:uADsZpTKFAtp8SLK+hMwSaa+X+JiERHtd4sQAFmXeMo=
273+
github.com/testcontainers/testcontainers-go v0.35.0/go.mod h1:oEVBj5zrfJTrgjwONs1SsRbnBtH9OKl+IGl3UMcr2B4=
272274
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0 h1:c51aBXT3v2HEBVarmaBnsKzvgZjC5amn0qsj8Naqi50=
273275
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0/go.mod h1:EWP75ogLQU4M4L8U+20mFipjV4WIR9WtlMXSB6/wiuc=
274276
github.com/testcontainers/testcontainers-go/modules/redis v0.34.0 h1:HkkKZPi6W2I+ywqplvnKOYRBKXQgpdxErBbdgx8F8nw=

0 commit comments

Comments
 (0)