Skip to content

Commit

Permalink
feat: default role for dex support (#1559)
Browse files Browse the repository at this point in the history
Kuberpult changes to support Dex. We created a configurable default user
that is selected if it is enabled and dex does not return any roles
associated with a user after authentication. This is done as we haven't
retrieved the groups from google thorugh dex yet.

---------

Co-authored-by: Oliver Breitwieser <[email protected]>
Co-authored-by: Sven Urbanski <[email protected]>
  • Loading branch information
3 people authored May 17, 2024
1 parent 3825e7a commit 7dc12dd
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
4 changes: 3 additions & 1 deletion charts/kuberpult/templates/cd-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ spec:
{{- if .Values.auth.dexAuth.enabled }}
- name: KUBERPULT_DEX_RBAC_POLICY_PATH
value: /kuberpult-rbac/policy.csv
- name: KUBERPULT_DEX_DEFAULT_ROLE_ENABLED
value: "{{ .Values.auth.dexAuth.defaultRoleEnabled }}"
{{- end }}
- name: KUBERPULT_AZURE_ENABLE_AUTH
value: "{{ .Values.auth.azureAuth.enabled }}"
Expand Down Expand Up @@ -282,7 +284,7 @@ spec:
{{- if .Values.auth.dexAuth.enabled }}
- name: kuberpult-rbac
mountPath: /kuberpult-rbac
{{- end }}
{{- end }}
{{- if .Values.dogstatsdMetrics.enabled }}
- name: dsdsocket
mountPath: {{ .Values.dogstatsdMetrics.hostSocketPath }}
Expand Down
3 changes: 2 additions & 1 deletion charts/kuberpult/templates/frontend-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ spec:
- name: KUBERPULT_DEX_CLIENT_ID
value: "{{ .Values.auth.dexAuth.clientId }}"
- name: KUBERPULT_DEX_CLIENT_SECRET
value: "{{ .Values.auth.dexAuth.clientSecret }}"
value:
{{- toYaml .Values.auth.dexAuth.clientSecret | nindent 12 }}
- name: KUBERPULT_DEX_BASE_URL
value: "{{ .Values.auth.dexAuth.baseURL }}"
- name: KUBERPULT_DEX_SCOPES
Expand Down
3 changes: 3 additions & 0 deletions charts/kuberpult/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ auth:
enabled: false
# Indicates if dex is to be installed. If you want to use your own Dex instance do not enable this flag.
installDex: false
# If kuberpult cannot find a role in the dex response, it will use the role "default".
# This is only recommended for when you want the simplest possible setup, or for testing purposes.
defaultRoleEnabled: false
# If using e.g. GCP IAP cluster internal communication to Dex is necessary as otherwise its endpoints cannot be accessed by the frontend service
# If enabled, kuberpult communicates with dex over http, not https
useClusterInternalCommunicationToDex: false
Expand Down
11 changes: 10 additions & 1 deletion pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ type GrpcContextReader interface {
}

type DexGrpcContextReader struct {
DexEnabled bool
DexEnabled bool
DexDefaultRoleEnabled bool
}

type DummyGrpcContextReader struct {
Expand Down Expand Up @@ -143,7 +144,15 @@ func (x *DexGrpcContextReader) ReadUserFromGrpcContext(ctx context.Context) (*Us
// RBAC Role of the user. only mandatory if DEX is enabled.
if x.DexEnabled {
rolesInHeader := md.Get(HeaderUserRole)

if len(rolesInHeader) == 0 {
if x.DexDefaultRoleEnabled {
u.DexAuthContext = &DexAuthContext{
Role: "default",
}
logger.FromContext(ctx).Warn("role undefined but dex is enabled. Default user role enabled. Proceeding with default role.")
return u, nil
}
return nil, grpc.AuthError(ctx, fmt.Errorf("extract: role undefined but dex is enabled"))
}
userRole, err := Decode64(rolesInHeader[0])
Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/dex.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (a *DexAppClient) handleCallback(w http.ResponseWriter, r *http.Request) {

idToken, err := ValidateOIDCToken(ctx, a.IssuerURL, idTokenRAW, a.ClientID, a.UseClusterInternalCommunication)
if err != nil {
http.Error(w, "failed to verify the token", http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("failed to verify the token: %v", err), http.StatusInternalServerError)
return
}

Expand Down
3 changes: 2 additions & 1 deletion services/cd-service/pkg/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type Config struct {
DbUserPassword string `default:"" split_words:"true"`
DbAuthProxyPort string `default:"5432" split_words:"true"`
DbMigrationsLocation string `default:"" split_words:"true"`
DexDefaultRoleEnabled bool `default:"false" split_words:"true"`
DbWriteEslTableOnly bool `default:"false" split_words:"true"`
}

Expand Down Expand Up @@ -136,7 +137,7 @@ func RunServer() {
}
reader = &auth.DummyGrpcContextReader{Role: c.DexMockRole}
} else {
reader = &auth.DexGrpcContextReader{DexEnabled: c.DexEnabled}
reader = &auth.DexGrpcContextReader{DexEnabled: c.DexEnabled, DexDefaultRoleEnabled: c.DexDefaultRoleEnabled}
}
dexRbacPolicy, err := auth.ReadRbacPolicy(c.DexEnabled, c.DexRbacPolicyPath)
if err != nil {
Expand Down

0 comments on commit 7dc12dd

Please sign in to comment.