Skip to content

Commit 9c1c121

Browse files
committed
add roles
1 parent 28960d7 commit 9c1c121

File tree

6 files changed

+94
-8
lines changed

6 files changed

+94
-8
lines changed

plugins/jumpcloud/pkg/jc/jc.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/go-http-utils/headers"
1314
"github.com/pkg/errors"
1415
"github.com/samber/lo"
1516
"google.golang.org/grpc/codes"
@@ -37,9 +38,9 @@ func NewJumpCloudClient(ctx context.Context, apiKey string) (*JumpCloudClient, e
3738
apiKey: apiKey,
3839
baseURL: base,
3940
headers: map[string]string{
40-
"Content-Type": "application/json",
41-
"Accept": "application/json",
42-
apiKeyHeader: apiKey,
41+
headers.ContentType: "application/json",
42+
headers.Accept: "application/json",
43+
apiKeyHeader: apiKey,
4344
},
4445
timeout: defaultConnectionTimeout,
4546
}

plugins/keycloak/pkg/app/assets/transform_template.tmpl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@
3939
}
4040
}
4141
{{ end }}
42+
{{ if eq $.type "role" }}
43+
{
44+
"id": "{{ $.id }}",
45+
"type": "role",
46+
"display_name": "{{ $.name }}",
47+
"properties": {
48+
{{ if .description }}
49+
"description": "{{ $.description }}"
50+
{{ end }}
51+
}
52+
}
53+
{{ end }}
4254
],
4355
"relations": [
4456
{{ if eq $.type "user" }}
@@ -69,5 +81,17 @@
6981
}
7082
{{ end }}
7183
{{ end }}
84+
{{ if eq $.type "role" }}
85+
{{ range $i, $user := $.users }}
86+
{{ if $i }},{{ end }}
87+
{
88+
"object_type": "role",
89+
"object_id": "{{ $.id }}",
90+
"relation": "member",
91+
"subject_type": "user",
92+
"subject_id": "{{ $user.id }}"
93+
}
94+
{{ end }}
95+
{{ end }}
7296
]
7397
}

plugins/keycloak/pkg/app/fetch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
type FetchCmd struct {
1313
kc.KeycloakClientConfig
1414
Groups bool `short:"g" help:"Retrieve keycloak groups" env:"KEYCLOAK_GROUPS" default:"false"`
15+
Roles bool `short:"r" help:"Retrieve keycloak roles" env:"KEYCLOAK_ROLES" default:"false"`
1516
}
1617

1718
func (cmd *FetchCmd) Run(ctx *cc.CommonCtx) error {
@@ -25,7 +26,7 @@ func (cmd *FetchCmd) Run(ctx *cc.CommonCtx) error {
2526
return err
2627
}
2728

28-
fetcher = fetcher.WithGroups(cmd.Groups)
29+
fetcher = fetcher.WithGroups(cmd.Groups).WithRoles(cmd.Roles)
2930

3031
return fetcher.Fetch(ctx.Context, os.Stdout, common.NewErrorWriter(os.Stderr))
3132
}

plugins/keycloak/pkg/fetch/fetch.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
type Fetcher struct {
1414
kcc *kc.KeycloakClient
1515
Groups bool
16+
Roles bool
1617
}
1718

1819
func New(client *kc.KeycloakClient) (*Fetcher, error) {
@@ -26,6 +27,11 @@ func (f *Fetcher) WithGroups(groups bool) *Fetcher {
2627
return f
2728
}
2829

30+
func (f *Fetcher) WithRoles(roles bool) *Fetcher {
31+
f.Roles = roles
32+
return f
33+
}
34+
2935
func (f *Fetcher) Fetch(ctx context.Context, outputWriter io.Writer, errorWriter common.ErrorWriter) error {
3036
writer := js.NewJSONArrayWriter(outputWriter)
3137
defer writer.Close()
@@ -63,10 +69,16 @@ func (f *Fetcher) Fetch(ctx context.Context, outputWriter io.Writer, errorWriter
6369
}
6470
}
6571

72+
if f.Roles {
73+
if err := f.fetchRoles(ctx, writer, errorWriter); err != nil {
74+
return err
75+
}
76+
}
77+
6678
return nil
6779
}
6880

69-
func (f *Fetcher) fetchGroups(ctx context.Context,
81+
func (f *Fetcher) fetchGroups(ctx context.Context, //nolint:dupl
7082
writer *js.JSONArrayWriter,
7183
errorWriter common.ErrorWriter,
7284
) error {
@@ -106,3 +118,44 @@ func (f *Fetcher) fetchGroups(ctx context.Context,
106118

107119
return nil
108120
}
121+
122+
func (f *Fetcher) fetchRoles(ctx context.Context, //nolint:dupl
123+
writer *js.JSONArrayWriter,
124+
errorWriter common.ErrorWriter,
125+
) error {
126+
roles, err := f.kcc.ListRoles(ctx)
127+
if err != nil {
128+
errorWriter.Error(err)
129+
return err
130+
}
131+
132+
for _, role := range roles {
133+
role.Type = "role"
134+
roleBytes, err := json.Marshal(role)
135+
errorWriter.Error(err)
136+
137+
var obj map[string]any
138+
if err := json.Unmarshal(roleBytes, &obj); err != nil {
139+
errorWriter.Error(err)
140+
continue
141+
}
142+
143+
usersInRole, err := f.kcc.GetUsersOfRole(ctx, role.Name)
144+
errorWriter.Error(err)
145+
146+
usersInRoleBytes, err := json.Marshal(usersInRole)
147+
errorWriter.Error(err)
148+
149+
var users []map[string]any
150+
if err := json.Unmarshal(usersInRoleBytes, &users); err != nil {
151+
errorWriter.Error(err)
152+
}
153+
154+
obj["users"] = users
155+
156+
err = writer.Write(obj)
157+
errorWriter.Error(err)
158+
}
159+
160+
return nil
161+
}

plugins/keycloak/pkg/kc/kc.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"time"
1111

12+
"github.com/go-http-utils/headers"
1213
"github.com/golang-jwt/jwt/v5"
1314
"github.com/pkg/errors"
1415
"golang.org/x/oauth2"
@@ -56,9 +57,9 @@ func NewKeycloakClient(ctx context.Context, cfg *KeycloakClientConfig) (*Keycloa
5657
}
5758

5859
headers := map[string]string{
59-
"Content-Type": "application/json",
60-
"Accept": "application/json",
61-
"Authorization": "Bearer " + token.AccessToken,
60+
headers.ContentType: "application/json",
61+
headers.Accept: "application/json",
62+
headers.Authorization: "Bearer " + token.AccessToken,
6263
}
6364

6465
return &KeycloakClient{

plugins/keycloak/test/manifest.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ types:
2828
relations:
2929
### display_name: group#member ###
3030
member: user | group#member
31+
32+
### display_name: Role ###
33+
role:
34+
relations:
35+
### display_name: role#member ###
36+
member: user | role#member

0 commit comments

Comments
 (0)