Skip to content

Commit a2a8ffc

Browse files
Include GitLab as a Catalog Provider when RBAC enabled
Signed-off-by: David Perza <[email protected]>
1 parent f7e6cdd commit a2a8ffc

File tree

202 files changed

+52772
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

202 files changed

+52772
-15
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/quay/claircore v1.5.39
1717
github.com/spf13/cobra v1.9.1
1818
github.com/spf13/pflag v1.0.7
19+
gitlab.com/gitlab-org/api/client-go v0.137.0
1920
gopkg.in/yaml.v3 v3.0.1
2021
helm.sh/helm/v3 v3.18.4
2122
k8s.io/api v0.33.3
@@ -71,7 +72,9 @@ require (
7172
github.com/gosuri/uitable v0.0.4 // indirect
7273
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
7374
github.com/hashicorp/errwrap v1.1.0 // indirect
75+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
7476
github.com/hashicorp/go-multierror v1.1.1 // indirect
77+
github.com/hashicorp/go-retryablehttp v0.7.8 // indirect
7578
github.com/huandu/xstrings v1.5.0 // indirect
7679
github.com/inconshreveable/mousetrap v1.1.0 // indirect
7780
github.com/invopop/jsonschema v0.13.0 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,14 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.24.0/go.mod h1:qztMSjm835F2bXf+5HKA
151151
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
152152
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
153153
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
154+
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
155+
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
156+
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
157+
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
154158
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
155159
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
160+
github.com/hashicorp/go-retryablehttp v0.7.8 h1:ylXZWnqa7Lhqpk0L1P1LzDtGcCR0rPVUrx/c8Unxc48=
161+
github.com/hashicorp/go-retryablehttp v0.7.8/go.mod h1:rjiScheydd+CxvumBsIrFKlx3iS0jrZ7LvzFGFmuKbw=
156162
github.com/hashicorp/golang-lru/arc/v2 v2.0.5 h1:l2zaLDubNhW4XO3LnliVj0GXO3+/CGNJAg1dcN2Fpfw=
157163
github.com/hashicorp/golang-lru/arc/v2 v2.0.5/go.mod h1:ny6zBSQZi2JxIeYcv7kt2sH2PXJtirBN7RDhRpxPkxU=
158164
github.com/hashicorp/golang-lru/v2 v2.0.5 h1:wW7h1TG88eUIJ2i69gaE3uNVtEPIagzhGvHgwfx2Vm4=
@@ -334,6 +340,8 @@ github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT0
334340
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
335341
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
336342
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
343+
gitlab.com/gitlab-org/api/client-go v0.137.0 h1:H26yL44qnb38Czl20pEINCJrcj63W6/BX8iKPVUKQP0=
344+
gitlab.com/gitlab-org/api/client-go v0.137.0/go.mod h1:AcAYES3lfkIS4zhso04S/wyUaWQmDYve2Fd9AF7C6qc=
337345
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
338346
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
339347
go.opentelemetry.io/contrib/bridges/prometheus v0.57.0 h1:UW0+QyeyBVhn+COBec3nGhfnFe5lwB0ic1JBVjzhk0w=

installer/charts/tssc-dh/templates/app-config-content.yaml

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ app:
1818
{{- $azureSecretObj := (lookup "v1" "Secret" $integrationNamespace "tssc-azure-integration") }}
1919
{{- $azureSecretData := ($azureSecretObj.data | default dict) }}
2020

21+
# Validation
22+
{{- if and (not $githubSecretObj) (eq .Values.developerHub.authProvider "github") }}
23+
{{- fail (printf "Github Integration required for github auth provider") }}
24+
{{- else if and (not $gitlabSecretObj) (eq .Values.developerHub.authProvider "gitlab") }}
25+
{{- fail (printf "Gitlab Integration required for gitlab auth provider") }}
26+
{{- else if and (not $azureSecretObj) (eq .Values.developerHub.authProvider "microsoft") }}
27+
{{- fail (printf "Azure Integration required for microsoft auth provider") }}
28+
{{- else if not (has .Values.developerHub.authProvider (list "github" "gitlab" "microsoft")) }}
29+
{{- fail (printf "Auth provider %s is not supported, set it to github, gitlab, or microsoft" .Values.developerHub.authProvider) }}
30+
{{- end }}
31+
2132
{{- if $argocdSecretData }}
2233
argocd:
2334
appLocatorMethods:
@@ -43,7 +54,7 @@ auth:
4354
environment: production
4455
providers:
4556
{{- $signInPage := "" }}
46-
{{- if $azureSecretData }}
57+
{{- if eq .Values.developerHub.authProvider "microsoft" }}
4758
{{- if and $azureSecretData.clientId $azureSecretData.clientSecret $azureSecretData.tenantId }}
4859
{{- $signInPage = "microsoft" }}
4960
microsoft:
@@ -53,7 +64,7 @@ auth:
5364
tenantId: ${AZURE__TENANT__ID}
5465
{{- end }}
5566
{{- end }}
56-
{{- if $githubSecretObj }}
67+
{{- if eq .Values.developerHub.authProvider "github" }}
5768
{{- $signInPage = "github" }}
5869
github:
5970
production:
@@ -69,8 +80,8 @@ auth:
6980
dangerouslyAllowSignInWithoutUserInCatalog: true
7081
{{- end }}
7182
{{- end }}
72-
{{- $gitlabSecretData := ($gitlabSecretObj.data | default dict) }}
73-
{{- if $gitlabSecretData }}
83+
{{- if eq .Values.developerHub.authProvider "gitlab" }}
84+
{{- $gitlabSecretData := ($gitlabSecretObj.data | default dict) }}
7485
{{- if and $gitlabSecretData.clientId $gitlabSecretData.clientSecret }}
7586
{{- $signInPage = "gitlab" }}
7687
gitlab:
@@ -97,8 +108,10 @@ backend:
97108
cors:
98109
origin: ${BACKEND_URL}
99110
catalog:
100-
{{- if and .Values.developerHub.RBAC.enabled $githubSecretObj }}
111+
{{- if .Values.developerHub.RBAC.enabled }}
101112
providers:
113+
{{- end }}
114+
{{- if eq .Values.developerHub.authProvider "github" }}
102115
githubOrg:
103116
id: production
104117
githubUrl: ${GITHUB__URL}
@@ -109,6 +122,17 @@ catalog:
109122
frequency: { minutes: 15 }
110123
timeout: { minutes: 5 }
111124
{{- end }}
125+
{{- if eq .Values.developerHub.authProvider "gitlab" }}
126+
gitlab:
127+
default:
128+
host: ${GITLAB__HOST}
129+
orgEnabled: true
130+
group: ${GITLAB__GROUP}
131+
schedule:
132+
initialDelay: { seconds: 30 }
133+
frequency: { minutes: 15 }
134+
timeout: { minutes: 5 }
135+
{{- end }}
112136
locations:
113137
- target: ${DEVELOPER_HUB__CATALOG__URL}
114138
type: url
@@ -174,17 +198,15 @@ jenkins:
174198
nexus:
175199
uiUrl: ${NEXUS__URL}
176200
{{- end }}
177-
{{- if .Values.developerHub.RBAC.enabled }}
201+
{{- if and .Values.developerHub.RBAC.enabled (has .Values.developerHub.authProvider (list "github" "gitlab")) }}
178202
permission:
179203
enabled: true
180-
{{- if $githubSecretObj }}
181204
rbac:
182205
admin:
183206
users:
184207
{{- range .Values.developerHub.RBAC.adminUsers }}
185208
- name: user:default/{{ . }}
186209
{{- end }}
187-
{{- end }}
188210
{{- else }}
189211
permission:
190212
enabled: false

installer/charts/tssc-dh/templates/extra-env.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ data:
7474
{{- $glSecretObj := (lookup "v1" "Secret" $integrationNamespace "tssc-gitlab-integration") -}}
7575
{{- $glSecretData := ($glSecretObj.data | default dict) -}}
7676
{{- if $glSecretData }}
77+
GITLAB__GROUP: "{{ $glSecretData.group }}"
7778
GITLAB__HOST: {{ $glSecretData.host }}
7879
GITLAB__TOKEN: "{{ $glSecretData.token }}"
7980
GITLAB__URL: {{ print "https://" ($glSecretData.host | b64dec) | b64enc }}
81+
GITLAB__USERNAME: {{ $glSecretData.username }}
8082
{{- if and $glSecretData.clientId $glSecretData.clientSecret }}
8183
GITLAB__APP__CLIENT__ID: {{ $glSecretData.clientId }}
8284
GITLAB__APP__CLIENT__SECRET: {{ $glSecretData.clientSecret }}

installer/charts/tssc-dh/templates/plugins-content.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,22 @@ plugins:
178178
#
179179
# RBAC
180180
#
181-
{{- $githubSecretObj := (lookup "v1" "Secret" $integrationNamespace "tssc-github-integration") }}
182-
{{- if and .Values.developerHub.RBAC.enabled $githubSecretObj }}
181+
{{- if .Values.developerHub.RBAC.enabled }}
183182
- package: ./dynamic-plugins/dist/backstage-community-plugin-rbac
184183
disabled: false
184+
{{- if eq .Values.developerHub.authProvider "github" }}
185185
- package: ./dynamic-plugins/dist/backstage-plugin-catalog-backend-module-github-org-dynamic
186186
disabled: false
187187
pluginConfig:
188188
catalog:
189189
githubOrg:
190190
githubUrl: ${GITHUB__URL}
191191
{{- end }}
192+
{{- if eq .Values.developerHub.authProvider "gitlab" }}
193+
- package: ./dynamic-plugins/dist/backstage-plugin-catalog-backend-module-gitlab-org-dynamic
194+
disabled: false
195+
{{- end }}
196+
{{- end }}
192197
#
193198
# Tech Docs
194199
#

installer/charts/values.yaml.tpl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,28 @@ integrations:
218218
{{- $catalogURL := required "Red Hat Developer Hub Catalog URL is required"
219219
$rhdh.Properties.catalogURL }}
220220

221+
{{- $authProvider := required "Auth Provider is required"
222+
$rhdh.Properties.authProvider }}
223+
224+
221225
developerHub:
222226
namespace: {{ $rhdh.Namespace }}
223227
ingressDomain: {{ $ingressDomain }}
224228
catalogURL: {{ $catalogURL }}
229+
authProvider: {{ $authProvider }}
225230
integrationSecrets:
226231
namespace: {{ .Installer.Namespace }}
227232
RBAC:
233+
enabled: {{ dig "Properties" "RBAC" "enabled" false $rhdh }}
234+
{{- if eq $authProvider "github" }}
228235
adminUsers:
229236
{{ dig "Properties" "RBAC" "adminUsers" (list "${GITHUB__USERNAME}") $rhdh | toYaml | indent 6 }}
230-
enabled: {{ dig "Properties" "RBAC" "enabled" false $rhdh }}
231237
orgs:
232238
{{ dig "Properties" "RBAC" "orgs" (list "${GITHUB__ORG}") $rhdh | toYaml | indent 6 }}
233-
239+
{{- else if eq $authProvider "gitlab" }}
240+
adminUsers:
241+
{{ dig "Properties" "RBAC" "adminUsers" (list "${GITLAB__USERNAME}") $rhdh | toYaml | indent 6 }}
242+
{{- end }}
234243
#
235244
# tssc-tpa-realm
236245
#

installer/config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ tssc:
2727
manageSubscription: true
2828
# namespacePrefixes:
2929
# - tssc-app
30+
authProvider: github
31+
# Possible values: github, gitlab, microsoft
3032
# RBAC:
3133
# adminUsers:
3234
# - myUsername
33-
# enabled: false
35+
# enabled: true
3436
# orgs:
3537
# - myOrg
3638
advancedClusterSecurity:

pkg/integrations/gitlab.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package integrations
22

33
import (
44
"context"
5+
"crypto/tls"
56
"fmt"
67
"log/slog"
8+
"net/http"
79

810
"github.com/redhat-appstudio/tssc/pkg/config"
911
"github.com/redhat-appstudio/tssc/pkg/k8s"
10-
1112
"github.com/spf13/cobra"
13+
gitlab "gitlab.com/gitlab-org/api/client-go"
1214
corev1 "k8s.io/api/core/v1"
1315
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1416
"k8s.io/apimachinery/pkg/types"
@@ -22,7 +24,8 @@ type GitLabIntegration struct {
2224
logger *slog.Logger // application logger
2325
kube *k8s.Kube // kubernetes client
2426

25-
force bool // overwrite the existing secret
27+
force bool // overwrite the existing secret
28+
insecure bool // Skips tls verification on api calls
2629

2730
host string // GitLab host
2831
clientId string // GitLab application client id
@@ -37,6 +40,8 @@ func (g *GitLabIntegration) PersistentFlags(c *cobra.Command) {
3740

3841
p.BoolVar(&g.force, "force", g.force,
3942
"Overwrite the existing secret")
43+
p.BoolVar(&g.insecure, "insecure", g.insecure,
44+
"Skips tls verification on api calls")
4045

4146
p.StringVar(&g.host, "host", g.host,
4247
"GitLab host, defaults to 'gitlab.com'")
@@ -60,6 +65,7 @@ func (g *GitLabIntegration) PersistentFlags(c *cobra.Command) {
6065
func (g *GitLabIntegration) log() *slog.Logger {
6166
return g.logger.With(
6267
"force", g.force,
68+
"insecure", g.insecure,
6369
"host", g.host,
6470
"clientId", g.clientId,
6571
"clientSecret-len", len(g.clientSecret),
@@ -126,11 +132,51 @@ func (g *GitLabIntegration) prepareSecret(
126132
return k8s.DeleteSecret(ctx, g.kube, g.secretName(cfg))
127133
}
128134

135+
// getCurrentGitLabUser gets the current user name authenticated with access token
136+
func (g *GitLabIntegration) getCurrentGitLabUser() (string, error) {
137+
url := fmt.Sprintf("https://%s", g.host)
138+
logger := g.log()
139+
140+
cl, err := gitlab.NewClient(g.token, gitlab.WithBaseURL(url))
141+
if err != nil {
142+
logger.Error("Error building gitlab client")
143+
return "", err
144+
}
145+
146+
if g.insecure {
147+
insecureTransport := &http.Transport{
148+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true, MinVersion: tls.VersionTLS12},
149+
}
150+
151+
hcl := &http.Client{Transport: insecureTransport}
152+
153+
cl, err = gitlab.NewClient(g.token, gitlab.WithBaseURL(url), gitlab.WithHTTPClient(hcl))
154+
if err != nil {
155+
logger.Error("Error building gitlab client")
156+
return "", err
157+
}
158+
}
159+
160+
user, _, err := cl.Users.CurrentUser()
161+
if err != nil {
162+
logger.Error("Error getting user")
163+
return "", err
164+
}
165+
166+
return user.Username, nil
167+
}
168+
129169
// store creates the secret with the integration data.
130170
func (g *GitLabIntegration) store(
131171
ctx context.Context,
132172
cfg *config.Config,
133173
) error {
174+
// Getting the user name
175+
username, err := g.getCurrentGitLabUser()
176+
if err != nil {
177+
return err
178+
}
179+
134180
secret := &corev1.Secret{
135181
ObjectMeta: metav1.ObjectMeta{
136182
Namespace: g.secretName(cfg).Namespace,
@@ -143,6 +189,7 @@ func (g *GitLabIntegration) store(
143189
"host": []byte(g.host),
144190
"token": []byte(g.token),
145191
"group": []byte(g.group),
192+
"username": []byte(username),
146193
},
147194
}
148195
logger := g.log().With(
@@ -185,6 +232,7 @@ func NewGitLabIntegration(
185232
kube: kube,
186233

187234
force: false,
235+
insecure: false,
188236
host: defaultPublicGitLabHost,
189237
clientId: "",
190238
clientSecret: "",

0 commit comments

Comments
 (0)