Skip to content

Commit 1b1a4a8

Browse files
Priya Wadhwatekton-robot
authored andcommitted
Add defaults and validation to configmap
1 parent 43122eb commit 1b1a4a8

File tree

5 files changed

+110
-37
lines changed

5 files changed

+110
-37
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ See [DEVELOPMENT.md](DEVELOPMENT.md) for a guide on how to build and deploy your
1313
### Configuration
1414

1515
Chains uses a `ConfigMap` called `chains-config` in the `tekton-chains` namespace for configuration.
16-
Supported keys include:
17-
18-
| Key | Description | Supported Values |
19-
| --- | --- | --- |
20-
| `artifacts.taskrun.format` | The format to store `TaskRun` payloads in. | `tekton` |
21-
| `artifacts.taskrun.storage` | The storage backend to store `TaskRun` signatures in. | `tekton`, `oci`, `gcs`, `docdb` |
22-
| `artifacts.taskrun.signer` | The signature backend to sign `Taskrun` payloads with. | `pgp`, `x509`, `kms` |
23-
| `artifacts.oci.format` | The format to store `OCI` payloads in. | `tekton`, `simplesigning` |
24-
| `artifacts.oci.storage` | The storage backend to store `OCI` signatures in. | `tekton`, `oci`, `gcs`, `docdb` |
25-
| `artifacts.oci.signer` | The signature backend to sign `OCI` payloads with. | `pgp`, `x509`, `kms` |
26-
| `signers.kms.kmsref` | The URI reference to a KMS service to use in `KMS` signers. | `gcpkms://projects/<project>/locations/<location>/keyRings/<keyring>/cryptoKeys/<key>`|
27-
| `storage.docdb.url` | The go-cloud URI reference to a docstore collection | `firestore://projects/<project>/databases/(default)/documents/<collection>?name_field=name`|
16+
Supported keys include (scroll right for defaults):
17+
18+
| Key | Description | Supported Values | Default |
19+
| --- | --- | --- | --- |
20+
| `artifacts.taskrun.format` | The format to store `TaskRun` payloads in. | `tekton` | `tekton` |
21+
| `artifacts.taskrun.storage` | The storage backend to store `TaskRun` signatures in. | `tekton`, `oci`, `gcs`, `docdb` | `tekton` |
22+
| `artifacts.taskrun.signer` | The signature backend to sign `Taskrun` payloads with. | `pgp`, `x509`, `kms` | `x509` |
23+
| `artifacts.oci.format` | The format to store `OCI` payloads in. | `tekton`, `simplesigning` | `simplesigning` |
24+
| `artifacts.oci.storage` | The storage backend to store `OCI` signatures in. | `tekton`, `oci`, `gcs`, `docdb` | `oci` |
25+
| `artifacts.oci.signer` | The signature backend to sign `OCI` payloads with. | `pgp`, `x509`, `kms` | `x509` |
26+
| `signers.kms.kmsref` | The URI reference to a KMS service to use in `KMS` signers. | `gcpkms://projects/<project>/locations/<location>/keyRings/<keyring>/cryptoKeys/<key>`| |
27+
| `storage.docdb.url` | The go-cloud URI reference to a docstore collection | `firestore://projects/<project>/databases/(default)/documents/<collection>?name_field=name`| |
2828

2929
### Overview
3030

pkg/config/store.go

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,33 +95,74 @@ const (
9595
chainsConfig = "chains-config"
9696
)
9797

98+
var defaults = map[string]string{
99+
taskrunFormatKey: "tekton",
100+
taskrunStorageKey: "tekton",
101+
taskrunSignerKey: "x509",
102+
ociFormatKey: "simplesigning",
103+
ociStorageKey: "oci",
104+
ociSignerKey: "x509",
105+
}
106+
107+
var supportedValues = map[string][]string{
108+
taskrunFormatKey: {"tekton"},
109+
taskrunStorageKey: {"tekton", "oci", "gcs", "docdb"},
110+
taskrunSignerKey: {"pgp", "x509", "kms"},
111+
ociFormatKey: {"tekton", "simplesigning"},
112+
ociStorageKey: {"tekton", "oci", "gcs", "docdb"},
113+
ociSignerKey: {"pgp", "x509", "kms"},
114+
}
115+
98116
func parse(data map[string]string) Config {
99117
cfg := Config{}
100118

101119
// Artifact-specific configs
102120

103121
// TaskRuns
104-
cfg.Artifacts.TaskRuns.Format = data[taskrunFormatKey]
105-
cfg.Artifacts.TaskRuns.StorageBackend = data[taskrunStorageKey]
106-
cfg.Artifacts.TaskRuns.Signer = data[taskrunSignerKey]
122+
cfg.Artifacts.TaskRuns.Format = valueOrDefault(taskrunFormatKey, data)
123+
cfg.Artifacts.TaskRuns.StorageBackend = valueOrDefault(taskrunStorageKey, data)
124+
cfg.Artifacts.TaskRuns.Signer = valueOrDefault(taskrunSignerKey, data)
107125

108126
// OCI
109-
cfg.Artifacts.OCI.Format = data[ociFormatKey]
110-
cfg.Artifacts.OCI.StorageBackend = data[ociStorageKey]
111-
cfg.Artifacts.OCI.Signer = data[ociSignerKey]
127+
cfg.Artifacts.OCI.Format = valueOrDefault(ociFormatKey, data)
128+
cfg.Artifacts.OCI.StorageBackend = valueOrDefault(ociStorageKey, data)
129+
cfg.Artifacts.OCI.Signer = valueOrDefault(ociSignerKey, data)
112130

113131
// Storage level configs
114132

115-
cfg.Storage.GCS.Bucket = data[gcsBucketKey]
116-
cfg.Storage.OCI.Repository = data[ociRepositoryKey]
117-
cfg.Storage.OCI.Insecure = (data[ociRepositoryInsecureKey] == "true")
118-
cfg.Storage.DocDB.URL = data[docDBUrlKey]
133+
cfg.Storage.GCS.Bucket = valueOrDefault(gcsBucketKey, data)
134+
cfg.Storage.OCI.Repository = valueOrDefault(ociRepositoryKey, data)
135+
cfg.Storage.OCI.Insecure = (valueOrDefault(ociRepositoryInsecureKey, data) == "true")
136+
cfg.Storage.DocDB.URL = valueOrDefault(docDBUrlKey, data)
119137

120-
cfg.Signers.KMS.KMSRef = data[kmsSignerKMSRef]
138+
cfg.Signers.KMS.KMSRef = valueOrDefault(kmsSignerKMSRef, data)
121139

122140
return cfg
123141
}
124142

143+
func valueOrDefault(key string, data map[string]string) string {
144+
if v, ok := data[key]; ok {
145+
if validate(key, v) {
146+
return v
147+
}
148+
}
149+
return defaults[key]
150+
}
151+
152+
func validate(key, value string) bool {
153+
vals, ok := supportedValues[key]
154+
// if it doesn't exist in supportedValues, we don't validate
155+
if !ok {
156+
return true
157+
}
158+
for _, v := range vals {
159+
if v == value {
160+
return true
161+
}
162+
}
163+
return false
164+
}
165+
125166
type ConfigStore struct {
126167
name string
127168
config atomic.Value

pkg/config/store_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,42 @@ func Test_parse(t *testing.T) {
111111
})
112112
}
113113
}
114+
115+
func TestValueOrDefault(t *testing.T) {
116+
tests := []struct {
117+
description string
118+
key string
119+
value string
120+
expected string
121+
}{
122+
{
123+
description: "valid key set to default",
124+
key: ociFormatKey,
125+
value: "simplesigning",
126+
expected: "simplesigning",
127+
}, {
128+
description: "valid key not set to default",
129+
key: ociFormatKey,
130+
value: "tekton",
131+
expected: "tekton",
132+
}, {
133+
description: "invalid value with default",
134+
key: ociFormatKey,
135+
value: "invalid",
136+
expected: "simplesigning",
137+
}, {
138+
description: "key with no default",
139+
key: gcsBucketKey,
140+
value: "bucket",
141+
expected: "bucket",
142+
},
143+
}
144+
for _, test := range tests {
145+
t.Run(test.description, func(t *testing.T) {
146+
got := valueOrDefault(test.key, map[string]string{test.key: test.value})
147+
if got != test.expected {
148+
t.Fatalf("got (%s) is not expected (%s)", got, test.expected)
149+
}
150+
})
151+
}
152+
}

test/clients.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ func createNamespace(ctx context.Context, t *testing.T, namespace string, kubeCl
121121
t.Logf("Create namespace %s to deploy to", namespace)
122122
if _, err := kubeClient.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{
123123
ObjectMeta: metav1.ObjectMeta{
124-
Name: namespace,
124+
Name: namespace,
125+
Labels: map[string]string{"chains": "integration-testing"},
125126
},
126127
}, metav1.CreateOptions{}); err != nil {
127128
t.Fatalf("Failed to create namespace %s for tests: %s", namespace, err)

test/e2e_test.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,10 @@ func TestTektonStorage(t *testing.T) {
6060

6161
// Setup the right config.
6262
resetConfig := setConfigMap(ctx, t, c, map[string]string{
63-
"artifacts.taskrun.storage": "tekton",
64-
"artifacts.taskrun.signer": "pgp",
65-
"artifacts.taskrun.format": "tekton",
66-
"artifacts.oci.format": "tekton",
67-
"artifacts.oci.storage": "tekton",
68-
"artifacts.oci.signer": "pgp",
63+
"artifacts.taskrun.signer": "pgp",
64+
"artifacts.oci.format": "tekton",
65+
"artifacts.oci.storage": "tekton",
66+
"artifacts.oci.signer": "pgp",
6967
})
7068
defer resetConfig()
7169

@@ -119,10 +117,7 @@ func TestOCISigning(t *testing.T) {
119117
defer cleanup()
120118

121119
// Setup the right config.
122-
resetConfig := setConfigMap(ctx, t, c, map[string]string{
123-
"artifacts.oci.format": "simplesigning",
124-
"artifacts.oci.storage": "tekton",
125-
"artifacts.oci.signer": "x509"})
120+
resetConfig := setConfigMap(ctx, t, c, map[string]string{"artifacts.oci.storage": "tekton"})
126121

127122
defer resetConfig()
128123

@@ -213,9 +208,6 @@ func TestOCIStorage(t *testing.T) {
213208
defer cleanup()
214209

215210
resetConfig := setConfigMap(ctx, t, c, map[string]string{
216-
"artifacts.taskrun.storage": "oci",
217-
"artifacts.taskrun.signer": "x509",
218-
"artifacts.taskrun.format": "simplesigning",
219211
"storage.oci.repository.insecure": "true",
220212
})
221213
defer resetConfig()

0 commit comments

Comments
 (0)