Skip to content

Commit 22f7d36

Browse files
authored
Gather installed PSP names (openshift#489)
1 parent cbd3d96 commit 22f7d36

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

docs/gathered-data.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,19 @@ Response see https://docs.okd.io/latest/rest_api/policy_apis/poddisruptionbudget
585585
* 4.6+
586586

587587

588+
## PodSecurityPolicies
589+
590+
gathers the names of installed PodSecurityPolicies
591+
592+
The Kubernetes API https://github.com/kubernetes/client-go/blob/v12.0.0/kubernetes/typed/policy/v1beta1/podsecuritypolicy.go#L76
593+
594+
* Location in archive: config/psp_names.json
595+
* See: docs/insights-archive-sample/config/psp_names.json
596+
* Id in config: psps
597+
* Since versions:
598+
* 4.10+
599+
600+
588601
## SAPConfig
589602

590603
collects selected security context constraints
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
"100-psp",
3+
"next-psp-name"
4+
]

pkg/gatherers/clusterconfig/clusterconfig_gatherer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ var gatheringFunctions = map[string]gatheringFunction{
8686
"pod_network_connectivity_checks": failableFunc((*Gatherer).GatherPNCC),
8787
"machine_autoscalers": failableFunc((*Gatherer).GatherMachineAutoscalers),
8888
"openshift_logging": failableFunc((*Gatherer).GatherOpenshiftLogging),
89+
"psps": failableFunc((*Gatherer).GatherPodSecurityPolicies),
8990
}
9091

9192
func New(
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package clusterconfig
2+
3+
import (
4+
"context"
5+
6+
"github.com/openshift/insights-operator/pkg/record"
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
policyclient "k8s.io/client-go/kubernetes/typed/policy/v1beta1"
9+
)
10+
11+
// GatherPodSecurityPolicies gathers the names of installed PodSecurityPolicies
12+
//
13+
// The Kubernetes API https://github.com/kubernetes/client-go/blob/v12.0.0/kubernetes/typed/policy/v1beta1/podsecuritypolicy.go#L76
14+
//
15+
// * Location in archive: config/psp_names.json
16+
// * See: docs/insights-archive-sample/config/psp_names.json
17+
// * Id in config: psps
18+
// * Since versions:
19+
// * 4.10+
20+
func (g *Gatherer) GatherPodSecurityPolicies(ctx context.Context) ([]record.Record, []error) {
21+
gatherPolicyClient, err := policyclient.NewForConfig(g.gatherKubeConfig)
22+
if err != nil {
23+
return nil, []error{err}
24+
}
25+
26+
return gatherPodSecurityPolicies(ctx, gatherPolicyClient)
27+
}
28+
29+
func gatherPodSecurityPolicies(ctx context.Context, policyClient policyclient.PolicyV1beta1Interface) ([]record.Record, []error) {
30+
psps, err := policyClient.PodSecurityPolicies().List(ctx, metav1.ListOptions{})
31+
if err != nil {
32+
return nil, []error{err}
33+
}
34+
pspNames := make([]string, 0, len(psps.Items))
35+
for i := range psps.Items {
36+
psp := psps.Items[i]
37+
pspNames = append(pspNames, psp.Name)
38+
}
39+
return []record.Record{{
40+
Name: "config/psp_names",
41+
Item: record.JSONMarshaller{Object: pspNames},
42+
}}, nil
43+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package clusterconfig
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/openshift/insights-operator/pkg/record"
8+
"github.com/stretchr/testify/assert"
9+
policyv1beta1 "k8s.io/api/policy/v1beta1"
10+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
kubefake "k8s.io/client-go/kubernetes/fake"
12+
)
13+
14+
var (
15+
psp1 *policyv1beta1.PodSecurityPolicy = &policyv1beta1.PodSecurityPolicy{
16+
ObjectMeta: v1.ObjectMeta{Name: "psp-1"},
17+
}
18+
psp2 *policyv1beta1.PodSecurityPolicy = &policyv1beta1.PodSecurityPolicy{
19+
ObjectMeta: v1.ObjectMeta{Name: "psp-2"},
20+
}
21+
)
22+
23+
func Test_PodSecurityPolicies_Gather(t *testing.T) {
24+
coreClient := kubefake.NewSimpleClientset()
25+
ctx := context.Background()
26+
records, errs := gatherPodSecurityPolicies(ctx, coreClient.PolicyV1beta1())
27+
assert.Empty(t, errs, "Unexpected errors: %#v", errs)
28+
assert.Len(t, records, 1)
29+
s, ok := records[0].Item.(record.JSONMarshaller).Object.([]string)
30+
assert.True(t, ok, "Unexpected data format. Expecting an array of strings")
31+
assert.Equal(t, s, []string{}, "Expecting an empty array")
32+
33+
// create some psps
34+
_, err := coreClient.PolicyV1beta1().PodSecurityPolicies().Create(ctx, psp1, v1.CreateOptions{})
35+
assert.NoError(t, err, "Unexpected error when creating test PodSecurityPolicy")
36+
_, err = coreClient.PolicyV1beta1().PodSecurityPolicies().Create(ctx, psp2, v1.CreateOptions{})
37+
assert.NoError(t, err, "Unexpected error when creating test PodSecurityPolicy")
38+
39+
// check that the created PSPs are actually gathered
40+
records, errs = gatherPodSecurityPolicies(ctx, coreClient.PolicyV1beta1())
41+
assert.Empty(t, errs, "Unexpected errors: %#v", errs)
42+
assert.Len(t, records, 1)
43+
44+
s, ok = records[0].Item.(record.JSONMarshaller).Object.([]string)
45+
assert.True(t, ok, "Unexpected data format. Expecting an array of strings")
46+
assert.Equal(t, s, []string{"psp-1", "psp-2"}, "Expecting an empty array")
47+
}

0 commit comments

Comments
 (0)