Skip to content

Commit b958eb5

Browse files
chmoueltekton-robot
authored andcommitted
Add a setting to disallow access to all namespaces
With the cluster resolver we allow access to all namespaces by default if empty. This is not always desirable and we should have a way to only allow explicitly the namespaces that are allowed. Let the user configure the `blocked-namespaces` setting to `*` to disallow all namespaces by default and only allow access to namespaces with the `allowed-namespaces` setting. Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent dcd34c1 commit b958eb5

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

docs/cluster-resolver.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ for the name, namespace and defaults that the resolver ships with.
4040
| `default-kind` | The default resource kind to fetch if not specified in parameters. | `task`, `pipeline` |
4141
| `default-namespace` | The default namespace to fetch resources from if not specified in parameters. | `default`, `some-namespace` |
4242
| `allowed-namespaces` | An optional comma-separated list of namespaces which the resolver is allowed to access. Defaults to empty, meaning all namespaces are allowed. | `default,some-namespace`, (empty) |
43-
| `blocked-namespaces` | An optional comma-separated list of namespaces which the resolver is blocked from accessing. Defaults to empty, meaning all namespaces are allowed. | `default,other-namespace`, (empty) |
43+
| `blocked-namespaces` | An optional comma-separated list of namespaces which the resolver is blocked from accessing. If the value is a `*` all namespaces will be disallowed and allowed namespace will need to be explicitely listed in `allowed-namespaces`. Defaults to empty, meaning all namespaces are allowed. | `default,other-namespace`, `*`, (empty) |
4444

4545
## Usage
4646

pkg/resolution/resolver/cluster/resolver.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ func populateParamsWithDefaults(ctx context.Context, origParams []pipelinev1.Par
279279
return nil, fmt.Errorf("access to specified namespace %s is blocked", params[NamespaceParam])
280280
}
281281

282+
if conf[AllowedNamespacesKey] != "" && isInCommaSeparatedList(params[NamespaceParam], conf[AllowedNamespacesKey]) {
283+
return params, nil
284+
}
285+
286+
if conf[BlockedNamespacesKey] != "" && conf[BlockedNamespacesKey] == "*" {
287+
return nil, fmt.Errorf("only explicit allowed access to namespaces is allowed")
288+
}
289+
282290
if conf[AllowedNamespacesKey] != "" && !isInCommaSeparatedList(params[NamespaceParam], conf[AllowedNamespacesKey]) {
283291
return nil, fmt.Errorf("access to specified namespace %s is not allowed", params[NamespaceParam])
284292
}

pkg/resolution/resolver/cluster/resolver_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,30 @@ func TestValidateParamsFailure(t *testing.T) {
159159
},
160160
expectedErr: "access to specified namespace foo is blocked",
161161
},
162+
{
163+
name: "blocked by star",
164+
params: map[string]string{
165+
cluster.KindParam: "task",
166+
cluster.NamespaceParam: "foo",
167+
cluster.NameParam: "baz",
168+
},
169+
conf: map[string]string{
170+
cluster.BlockedNamespacesKey: "*",
171+
},
172+
expectedErr: "only explicit allowed access to namespaces is allowed",
173+
},
174+
{
175+
name: "blocked by star but allowed explicitly",
176+
params: map[string]string{
177+
cluster.KindParam: "task",
178+
cluster.NamespaceParam: "foo",
179+
cluster.NameParam: "baz",
180+
},
181+
conf: map[string]string{
182+
cluster.BlockedNamespacesKey: "*",
183+
cluster.AllowedNamespacesKey: "foo",
184+
},
185+
},
162186
}
163187

164188
for _, tc := range testCases {
@@ -178,6 +202,12 @@ func TestValidateParamsFailure(t *testing.T) {
178202
})
179203
}
180204
err := resolver.ValidateParams(ctx, asParams)
205+
if tc.expectedErr == "" {
206+
if err != nil {
207+
t.Fatalf("got unexpected error: %v", err)
208+
}
209+
return
210+
}
181211
if err == nil {
182212
t.Fatalf("got no error, but expected: %s", tc.expectedErr)
183213
}

0 commit comments

Comments
 (0)