Skip to content

Commit 7371ab2

Browse files
Microzuul CIGerrit Code Review
authored andcommitted
Merge "zuul connections: store some sensitive parameters in secrets"
2 parents 899fade + 16ab7d2 commit 7371ab2

File tree

7 files changed

+118
-6
lines changed

7 files changed

+118
-6
lines changed

api/v1/softwarefactory_types.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,13 @@ type SMTPConnection struct {
207207
DefaultTo string `json:"defaultTo,omitempty"`
208208
// [user](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.user)
209209
User string `json:"user,omitempty"`
210-
// [password](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.password)
210+
// DEPRECATED use `Secrets` instead to securely store this value [password](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.password)
211211
Password string `json:"password,omitempty"`
212212
// [use_starttls](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.use_starttls)
213213
TLS *bool `json:"tls,omitempty"`
214+
// Name of the secret which contains the following keys:
215+
// the [password](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.password)
216+
Secrets *string `json:"secrets,omitempty"`
214217
}
215218

216219
// Describes a Zuul connection using the [ElasticSearch driver](https://zuul-ci.org/docs/zuul/latest/drivers/elasticsearch.html#connection-configuration).
@@ -224,6 +227,10 @@ type ElasticSearchConnection struct {
224227
UseSSL *bool `json:"useSSL,omitempty"`
225228
// [verifyCerts](https://zuul-ci.org/docs/zuul/latest/drivers/elasticsearch.html#attr-%3CElasticsearch%20connection%3E.verify_certs)
226229
VerifyCerts *bool `json:"verifyCerts,omitempty"`
230+
// If the connection requires basic authentication, the name of the secret containing the following keys:
231+
// * username
232+
// * password
233+
BasicAuthSecret *string `json:"basicAuthSecret,omitempty"`
227234
}
228235

229236
// The description of an OpenIDConnect authenticator, see [Zuul's authentication documentation](https://zuul-ci.org/docs/zuul/latest/configuration.html#authentication)

api/v1/zz_generated.deepcopy.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/sf.softwarefactory-project.io_softwarefactories.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,12 @@ spec:
511511
Describes a Zuul connection using the [ElasticSearch driver](https://zuul-ci.org/docs/zuul/latest/drivers/elasticsearch.html#connection-configuration).
512512
When an optional parameter is not specified then Zuul's defaults apply
513513
properties:
514+
basicAuthSecret:
515+
description: |-
516+
If the connection requires basic authentication, the name of the secret containing the following keys:
517+
* username
518+
* password
519+
type: string
514520
name:
515521
description: How the connection will be named in Zuul's
516522
configuration and appear in zuul-web
@@ -1104,11 +1110,17 @@ spec:
11041110
configuration and appear in zuul-web
11051111
type: string
11061112
password:
1107-
description: '[password](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.password)'
1113+
description: DEPRECATED use `Secrets` instead to securely
1114+
store this value [password](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.password)
11081115
type: string
11091116
port:
11101117
description: '[port](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.port)'
11111118
type: integer
1119+
secrets:
1120+
description: |-
1121+
Name of the secret which contains the following keys:
1122+
the [password](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.password)
1123+
type: string
11121124
server:
11131125
description: '[server](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.server)'
11141126
type: string

controllers/zuul.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77
"bytes"
88
_ "embed"
99
"fmt"
10+
"regexp"
1011
"strconv"
1112
"strings"
1213

1314
"golang.org/x/exp/maps"
1415
ini "gopkg.in/ini.v1"
1516
appsv1 "k8s.io/api/apps/v1"
1617
apiv1 "k8s.io/api/core/v1"
18+
"k8s.io/apimachinery/pkg/api/errors"
1719
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1820
"k8s.io/apimachinery/pkg/util/intstr"
1921
"k8s.io/utils/ptr"
@@ -1331,10 +1333,37 @@ func AddGitConnection(cfg *ini.File, name string, baseurl string, poolDelay int3
13311333

13321334
func (r *SFController) AddElasticSearchConnection(cfg *ini.File, conn sfv1.ElasticSearchConnection) {
13331335
section := "connection " + conn.Name
1336+
scheme := ""
1337+
uri := conn.URI
1338+
// crude clear-text basic auth check
1339+
if match, _ := regexp.MatchString("http[s]?://[^:]+:.+@.+", uri); match {
1340+
utils.LogI(fmt.Sprintf("It looks like elasticsearch connection %s has basic auth secrets stored in clear text. Use the 'basicAuthSecret' property instead", conn.Name))
1341+
}
1342+
if strings.HasPrefix(uri, "https://") {
1343+
scheme = "https://"
1344+
// TODO might not work with unicode URLs
1345+
uri = uri[len("https://"):]
1346+
}
1347+
if strings.HasPrefix(uri, "http://") {
1348+
scheme = "http://"
1349+
// TODO might not work with unicode URLs
1350+
uri = uri[len("http://"):]
1351+
}
1352+
if conn.BasicAuthSecret != nil {
1353+
password, passwordErr := r.GetSecretDataFromKey(*conn.BasicAuthSecret, "password")
1354+
// TODO we may also want to handle missing values in the secret
1355+
if errors.IsNotFound(passwordErr) {
1356+
utils.LogE(passwordErr, fmt.Sprintf("elasticsearch connection %s refers to a non-existing secret: %s ", conn.Name, *conn.BasicAuthSecret))
1357+
}
1358+
username, _ := r.GetSecretDataFromKey(*conn.BasicAuthSecret, "username")
1359+
uri = string(username) + ":" + string(password) + "@" + uri
1360+
}
1361+
uri = scheme + uri
1362+
13341363
cfg.NewSection(section)
13351364
cfg.Section(section).NewKey("driver", "elasticsearch")
13361365
cfg.Section(section).NewKey("ca_certs", "/etc/ssl/certs/ca-bundle.crt")
1337-
cfg.Section(section).NewKey("uri", conn.URI)
1366+
cfg.Section(section).NewKey("uri", uri)
13381367
// Optional fields (set as omitempty in ElasticSearchConnection struct definition)
13391368
if conn.UseSSL != nil && !*conn.UseSSL {
13401369
cfg.Section(section).NewKey("use_ssl", "false")
@@ -1362,8 +1391,17 @@ func (r *SFController) AddSMTPConnection(cfg *ini.File, conn sfv1.SMTPConnection
13621391
if conn.User != "" {
13631392
cfg.Section(section).NewKey("user", conn.User)
13641393
}
1365-
if conn.Password != "" {
1366-
cfg.Section(section).NewKey("password", conn.Password)
1394+
if conn.Secrets != nil {
1395+
password, passwordErr := r.GetSecretDataFromKey(*conn.Secrets, "password")
1396+
if errors.IsNotFound(passwordErr) {
1397+
utils.LogE(passwordErr, fmt.Sprintf("SMTP connection %s refers to a non-existing secret: %s ", conn.Name, *conn.Secrets))
1398+
}
1399+
cfg.Section(section).NewKey("password", string(password))
1400+
} else {
1401+
if conn.Password != "" {
1402+
utils.LogI("Deprecation Warning: SMTPConnection's Password field will disappear in a future version. Use Secrets instead")
1403+
cfg.Section(section).NewKey("password", conn.Password)
1404+
}
13671405
}
13681406
if conn.TLS != nil && !*conn.TLS {
13691407
cfg.Section(section).NewKey("use_starttls", "false")

doc/reference/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@ All notable changes to this project will be documented in this file.
66

77
### Added
88

9+
- zuul: add `BasicAuthSecret` parameter for elasticsearch connections. This parameter
10+
allows defining basic auth settings (username and password) and store them in a secret
11+
rather than in plain text in the software factort manifest.
12+
913
### Changed
1014

1115
- The default CPU limits have been reduced from 2000m to 500m to enable rollout on smaller cluster.
1216
- go version in go.mod is bumped to 1.24. Backward compatibility with earlier version is not guaranted.
1317
- zuul-* : bumped to 11.3.0-20250414-1 (using ubi9 latest images)
1418

1519
### Deprecated
20+
21+
- zuul: the `Password` parameter in SMTP connections is deprecated and will be removed
22+
in a future version. Use instead `Secrets` to point to a secret holding the password.
23+
1624
### Removed
1725
### Fixed
1826

doc/reference/api/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ _Appears in:_
8282
| `uri` _string_ | [uri](https://zuul-ci.org/docs/zuul/latest/drivers/elasticsearch.html#attr-%3CElasticsearch%20connection%3E.uri) | -|
8383
| `useSSL` _boolean_ | [useSSL](https://zuul-ci.org/docs/zuul/latest/drivers/elasticsearch.html#attr-%3CElasticsearch%20connection%3E.use_ssl) | -|
8484
| `verifyCerts` _boolean_ | [verifyCerts](https://zuul-ci.org/docs/zuul/latest/drivers/elasticsearch.html#attr-%3CElasticsearch%20connection%3E.verify_certs) | -|
85+
| `basicAuthSecret` _string_ | If the connection requires basic authentication, the name of the secret containing the following keys: * username * password | -|
8586

8687

8788
#### FluentBitForwarderSpec
@@ -379,8 +380,9 @@ _Appears in:_
379380
| `defaultFrom` _string_ | [default_from](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.default_from) | -|
380381
| `defaultTo` _string_ | [default_to](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.default_to) | -|
381382
| `user` _string_ | [user](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.user) | -|
382-
| `password` _string_ | [password](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.password) | -|
383+
| `password` _string_ | DEPRECATED use `Secrets` instead to securely store this value [password](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.password) | -|
383384
| `tls` _boolean_ | [use_starttls](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.use_starttls) | -|
385+
| `secrets` _string_ | Name of the secret which contains the following keys: the [password](https://zuul-ci.org/docs/zuul/latest/drivers/smtp.html#attr-%3Csmtp%20connection%3E.password) | -|
384386

385387

386388
#### Secret

roles/health-check/zuul-connections/tasks/main.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,39 @@
2424
dummy_elasticsearchconns:
2525
- name: dummy-elasticsearch-conn
2626
uri: http://test:9200
27+
basicAuthSecret: es-basicauth
2728
dummy_pagureconns:
2829
- name: dummy-pagure-conn
2930
secrets: pagureconnectionsecret
3031
dummy_smtpconns:
3132
- name: dummy-smtp-conn
3233
server: smtp.domain.com
34+
secrets: smtp-secret
35+
36+
- name: Create SMTP Connection Secret
37+
kubernetes.core.k8s:
38+
state: present
39+
definition:
40+
apiVersion: v1
41+
kind: Secret
42+
metadata:
43+
name: smtp-secret
44+
namespace: sf
45+
data:
46+
password: "{{ 'smtp-password' | b64encode }}"
47+
48+
- name: Create ElasticSearch Connection Secret
49+
kubernetes.core.k8s:
50+
state: present
51+
definition:
52+
apiVersion: v1
53+
kind: Secret
54+
metadata:
55+
name: es-basicauth
56+
namespace: sf
57+
data:
58+
username: "{{ 'es-username' | b64encode }}"
59+
password: "{{ 'es-password' | b64encode }}"
3360

3461
- name: Create Gerrit Connection Secret
3562
kubernetes.core.k8s:
@@ -135,6 +162,14 @@
135162
kubectl exec zuul-scheduler-0 -- grep "dummy-elasticsearch-conn" /etc/zuul/zuul.conf
136163
kubectl exec zuul-scheduler-0 -- grep "dummy-smtp-conn" /etc/zuul/zuul.conf
137164
165+
- name: Ensure ElasticSearch URI is configured with basic auth
166+
ansible.builtin.shell: |
167+
kubectl exec zuul-scheduler-0 -- grep "http://es-username:es-password@test:9200" /etc/zuul/zuul.conf
168+
169+
- name: Ensure SMTP password is present
170+
ansible.builtin.shell: |
171+
kubectl exec zuul-scheduler-0 -- grep "smtp-password" /etc/zuul/zuul.conf
172+
138173
- name: Ensure the new Zuul dummy gerrit secret exist in the scheduler's zuul.conf
139174
ansible.builtin.shell: |
140175
set -e

0 commit comments

Comments
 (0)