Skip to content

Commit c54d794

Browse files
authored
fix: added clientID/secret based access (#160)
* fix: added clientID/secret based access On-behalf-of: SAP [email protected] * fixed typo On-behalf-of: SAP [email protected] * chore: updated secret reading On-behalf-of: SAP [email protected] * chore: removed unused config variable On-behalf-of: SAP [email protected] * chore: updated client id in tests On-behalf-of: SAP [email protected]
1 parent bbd0565 commit c54d794

File tree

6 files changed

+25
-24
lines changed

6 files changed

+25
-24
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/kcp-dev/kcp/sdk v0.28.1-0.20250926104223-cec2e15f24c6
1414
github.com/kcp-dev/logicalcluster/v3 v3.0.5
1515
github.com/kcp-dev/multicluster-provider v0.2.0
16+
github.com/oapi-codegen/runtime v1.1.2
1617
github.com/openfga/api/proto v0.0.0-20250909173124-0ac19aac54f2
1718
github.com/openfga/language/pkg/go v0.2.0-beta.2.0.20251027165255-0f8f255e5f6c
1819
github.com/platform-mesh/account-operator v0.5.14
@@ -37,6 +38,7 @@ require (
3738
require (
3839
github.com/99designs/gqlgen v0.17.81 // indirect
3940
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
41+
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
4042
github.com/beorn7/perks v1.0.1 // indirect
4143
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
4244
github.com/cespare/xxhash/v2 v2.3.0 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ cel.dev/expr v0.24.0 h1:56OvJKSH3hDGL0ml5uSxZmz3/3Pq4tJ+fb1unVLAFcY=
22
cel.dev/expr v0.24.0/go.mod h1:hLPLo1W4QUmuYdA72RBX06QTs6MXw941piREPl3Yfiw=
33
github.com/99designs/gqlgen v0.17.81 h1:kCkN/xVyRb5rEQpuwOHRTYq83i0IuTQg9vdIiwEerTs=
44
github.com/99designs/gqlgen v0.17.81/go.mod h1:vgNcZlLwemsUhYim4dC1pvFP5FX0pr2Y+uYUoHFb1ig=
5+
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
56
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
67
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
78
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
89
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
10+
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
11+
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
912
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
1013
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
1114
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
1215
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
16+
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
1317
github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM=
1418
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
1519
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
@@ -108,6 +112,7 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
108112
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
109113
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
110114
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
115+
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
111116
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250728122101-adbf20db3e51 h1:l38RDS+VUMx9etvyaCgJIZa4nM7FaNevNubWN0kDZY4=
112117
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250728122101-adbf20db3e51/go.mod h1:rF1jfvUfPjFXs+HV/LN1BtPzAz1bfjJOwVa+hAVfroQ=
113118
github.com/kcp-dev/kcp/sdk v0.28.1-0.20250926104223-cec2e15f24c6 h1:bOR4mdLD24VCJRrHxmtTh21AdbbzkBBKkEh0ngL+XTc=
@@ -143,6 +148,8 @@ github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee h1:W5t00kpgFd
143148
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
144149
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
145150
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
151+
github.com/oapi-codegen/runtime v1.1.2 h1:P2+CubHq8fO4Q6fV1tqDBZHCwpVpvPg7oKiYzQgXIyI=
152+
github.com/oapi-codegen/runtime v1.1.2/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
146153
github.com/onsi/ginkgo/v2 v2.22.1 h1:QW7tbJAUDyVDVOM5dFa7qaybo+CRfR7bemlQUN6Z8aM=
147154
github.com/onsi/ginkgo/v2 v2.22.1/go.mod h1:S6aTpoRsSq2cZOd+pssHAlKW/Q/jZt6cPrPlnj4a1xM=
148155
github.com/onsi/gomega v1.36.2 h1:koNYke6TVk6ZmnyHrCXba/T/MoLBXFjeC1PtvYgw0A8=
@@ -199,6 +206,7 @@ github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
199206
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
200207
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
201208
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
209+
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
202210
github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AVEzs=
203211
github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
204212
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

internal/config/config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package config
22

33
type InviteConfig struct {
44
KeycloakBaseURL string `mapstructure:"invite-keycloak-base-url"`
5-
KeycloakClientID string `mapstructure:"invite-keycloak-client-id" default:"admin-cli"`
6-
KeycloakUser string `mapstructure:"invite-keycloak-user" default:"keycloak-admin"`
7-
KeycloakPasswordFile string `mapstructure:"invite-keycloak-password-file" default:"/var/run/secrets/keycloak/password"`
5+
KeycloakClientID string `mapstructure:"invite-keycloak-client-id" default:"security-operator"`
6+
KeycloakClientSecret string `mapstructure:"invite-keycloak-client-secret"`
87
}
98

109
// Config struct to hold the app config

internal/controller/invite_controller.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package controller // coverage-ignore
22

33
import (
44
"context"
5-
"os"
65

76
platformeshconfig "github.com/platform-mesh/golang-commons/config"
87
"github.com/platform-mesh/golang-commons/controller/lifecycle/builder"
@@ -24,12 +23,7 @@ type InviteReconciler struct {
2423
}
2524

2625
func NewInviteReconciler(ctx context.Context, mgr mcmanager.Manager, cfg *config.Config, log *logger.Logger) *InviteReconciler {
27-
pwd, err := os.ReadFile(cfg.Invite.KeycloakPasswordFile)
28-
if err != nil {
29-
log.Fatal().Err(err).Msg("Failed to read keycloak password file")
30-
}
31-
32-
inviteSubroutine, err := invite.New(ctx, cfg, mgr, string(pwd))
26+
inviteSubroutine, err := invite.New(ctx, cfg, mgr)
3327
if err != nil {
3428
log.Fatal().Err(err).Msg("Failed to create invite subroutine")
3529
}

internal/subroutine/invite/subroutine.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import (
1414
lifecyclesubroutine "github.com/platform-mesh/golang-commons/controller/lifecycle/subroutine"
1515
"github.com/platform-mesh/golang-commons/errors"
1616
"github.com/platform-mesh/golang-commons/logger"
17-
"golang.org/x/oauth2"
1817
ctrl "sigs.k8s.io/controller-runtime"
1918
"sigs.k8s.io/controller-runtime/pkg/client"
2019
mccontext "sigs.k8s.io/multicluster-runtime/pkg/context"
2120
mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager"
2221

2322
"github.com/platform-mesh/security-operator/api/v1alpha1"
2423
"github.com/platform-mesh/security-operator/internal/config"
24+
"golang.org/x/oauth2/clientcredentials"
2525
)
2626

2727
const (
@@ -48,29 +48,27 @@ type keycloakClient struct {
4848
ClientID string `json:"clientId,omitempty"`
4949
}
5050

51-
func New(ctx context.Context, cfg *config.Config, mgr mcmanager.Manager, pwd string) (*subroutine, error) {
51+
func New(ctx context.Context, cfg *config.Config, mgr mcmanager.Manager) (*subroutine, error) {
5252

5353
issuer := fmt.Sprintf("%s/realms/master", cfg.Invite.KeycloakBaseURL)
5454
provider, err := oidc.NewProvider(ctx, issuer)
5555
if err != nil {
5656
return nil, err
5757
}
5858

59-
config := oauth2.Config{
60-
ClientID: cfg.Invite.KeycloakClientID,
61-
Endpoint: provider.Endpoint(),
59+
cCfg := clientcredentials.Config{
60+
ClientID: cfg.Invite.KeycloakClientID,
61+
ClientSecret: cfg.Invite.KeycloakClientSecret,
62+
TokenURL: provider.Endpoint().TokenURL,
6263
}
6364

64-
token, err := config.PasswordCredentialsToken(ctx, cfg.Invite.KeycloakUser, pwd)
65-
if err != nil {
66-
return nil, err
67-
}
65+
httpClient := cCfg.Client(ctx)
6866

6967
return &subroutine{
7068
keycloakBaseURL: cfg.Invite.KeycloakBaseURL,
7169
baseDomain: cfg.BaseDomain,
7270
mgr: mgr,
73-
keycloak: config.Client(ctx, token),
71+
keycloak: httpClient,
7472
}, nil
7573
}
7674

internal/subroutine/invite/subroutine_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,10 @@ func TestSubroutineProcess(t *testing.T) {
350350
s, err := invite.New(ctx, &config.Config{
351351
Invite: config.InviteConfig{
352352
KeycloakBaseURL: srv.URL,
353-
KeycloakClientID: "admin-cli",
353+
KeycloakClientID: "security-operator",
354354
},
355355
BaseDomain: "portal.dev.local:8443",
356-
}, mgr, "password")
356+
}, mgr)
357357
assert.NoError(t, err)
358358

359359
l := testlogger.New()
@@ -382,9 +382,9 @@ func TestHelperFunctions(t *testing.T) {
382382
s, err := invite.New(ctx, &config.Config{
383383
Invite: config.InviteConfig{
384384
KeycloakBaseURL: srv.URL,
385-
KeycloakClientID: "admin-cli",
385+
KeycloakClientID: "security-operator",
386386
},
387-
}, nil, "password")
387+
}, nil)
388388
assert.NoError(t, err)
389389

390390
assert.Equal(t, "Invite", s.GetName())

0 commit comments

Comments
 (0)