mirror of
https://github.com/FairwindsOps/polaris.git
synced 2026-08-29 22:17:16 +00:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a73147a0d7 | ||
|
|
4980e6a731 | ||
|
|
1ddd2d985a | ||
|
|
727c6fa2c3 | ||
|
|
f531103fa0 | ||
|
|
d909dfd00b |
@@ -1,6 +1,6 @@
|
||||
# The action uses an own Dockerfile on purpose because the root Dockerfile takes way too long to build for an action
|
||||
|
||||
FROM alpine:3.10
|
||||
FROM alpine:3.17
|
||||
|
||||
RUN apk add --no-cache \
|
||||
bash \
|
||||
|
||||
@@ -17,4 +17,4 @@ mkdir polaris
|
||||
tar -xzf $TARGET_FILE -C polaris
|
||||
rm $TARGET_FILE
|
||||
echo "polaris" >> $GITHUB_PATH
|
||||
echo "::set-output name=version::$INPUT_VERSION"
|
||||
echo "version=$INPUT_VERSION" >> $GITHUB_OUTPUT
|
||||
@@ -11,7 +11,7 @@ jobs:
|
||||
stale:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/stale@v4
|
||||
- uses: actions/stale@v7
|
||||
with:
|
||||
exempt-issue-labels: pinned
|
||||
stale-pr-label: stale
|
||||
|
||||
@@ -7,7 +7,7 @@ jobs:
|
||||
build-int:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- name: Setup polaris
|
||||
uses: ./.github/actions/setup-polaris
|
||||
with:
|
||||
@@ -18,7 +18,7 @@ jobs:
|
||||
build-ext:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- name: Setup polaris
|
||||
uses: fairwindsops/polaris/.github/actions/setup-polaris@master
|
||||
with:
|
||||
|
||||
@@ -38,8 +38,9 @@ and network with fellow Kubernetes users.
|
||||
or
|
||||
[join the user group](https://www.fairwinds.com/open-source-software-user-group) to get involved!
|
||||
|
||||
<a href="https://www.fairwinds.com/t-shirt-offer?utm_source=polaris&utm_medium=polaris&utm_campaign=polaris-tshirt">
|
||||
<img src="https://www.fairwinds.com/hubfs/Doc_Banners/Fairwinds_OSS_User_Group_740x125_v6.png" alt="Love Fairwinds Open Source? Share your business email and job title and we'll send you a free Fairwinds t-shirt!" />
|
||||
<a href="https://insights.fairwinds.com/auth/register/">
|
||||
<img src="https://www.fairwinds.com/hubfs/Doc_Banners/Fairwinds_OSS_User_Group_740x125_v6.png"
|
||||
alt="Love Fairwinds Open Source? Automate Fairwinds Open Source for free with Fairwinds Insights. Click to learn more" />
|
||||
</a>
|
||||
|
||||
## Other Projects from Fairwinds
|
||||
|
||||
+13
-12
@@ -113,19 +113,20 @@ var fixCommand = &cobra.Command{
|
||||
|
||||
updatedYamlContent := ""
|
||||
if len(allMutations) > 0 {
|
||||
for _, resource := range kubeResources.Resources {
|
||||
key := fmt.Sprintf("%s/%s/%s", resource.Kind, resource.Resource.GetName(), resource.Resource.GetNamespace())
|
||||
fmt.Println("resource", key)
|
||||
mutations := allMutations[key]
|
||||
mutatedYamlContent, err := mutation.ApplyAllMutations(string(resource.OriginalObjectYAML), mutations)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error applying schema mutations to the resource %s: %v", key, err)
|
||||
os.Exit(1)
|
||||
for _, resources := range kubeResources.Resources {
|
||||
for _, resource := range resources {
|
||||
key := fmt.Sprintf("%s/%s/%s", resource.Kind, resource.Resource.GetName(), resource.Resource.GetNamespace())
|
||||
mutations := allMutations[key]
|
||||
mutatedYamlContent, err := mutation.ApplyAllMutations(string(resource.OriginalObjectYAML), mutations)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error applying schema mutations to the resource %s: %v", key, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if updatedYamlContent != "" {
|
||||
updatedYamlContent += "\n---\n"
|
||||
}
|
||||
updatedYamlContent += mutatedYamlContent
|
||||
}
|
||||
if updatedYamlContent != "" {
|
||||
updatedYamlContent += "\n---\n"
|
||||
}
|
||||
updatedYamlContent += mutatedYamlContent
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,19 @@ key | default | description
|
||||
`hostNetworkSet` | `warning` | Fails when `hostNetwork` attribute is configured.
|
||||
`hostPortSet` | `warning` | Fails when `hostPort` attribute is configured.
|
||||
`tlsSettingsMissing` | `warning` | Fails when an Ingress lacks TLS settings.
|
||||
`automountServiceAccountToken` | `ignore` | Fails if service account tokens are automounted to pods
|
||||
`clusterrolePodExecAttach` | `warning` | Fails if a ClusterRole is able to exec into pods
|
||||
`clusterrolebindingClusterAdmin` | `warning` | Fails if a ClusterRoleBinding is attached to the admin role
|
||||
`clusterrolebindingPodExecAttach` | `warning` | Fails if a ClusterRoleBinding is able to exec into pods
|
||||
`rolePodExecAttach` | `warning` | Fails if a Role is able to exec into pods
|
||||
`rolebindingClusterAdminClusterRole` | `warning` | Fails if a RoleBinding is attached to the cluster admin role
|
||||
`rolebindingClusterAdminRole` | `warning` | Fails if a RoleBinding is attached to the admin role
|
||||
`rolebindingClusterRolePodExecAttach` | `warning` | Fails if a RoleBinding is able to exec into pods in any namespace
|
||||
`rolebindingRolePodExecAttach` | `warning` | Fails if a RoleBinding is able to exec into pods in its namespace
|
||||
`linuxHardening` | `warning` | Fails if one of AppArmor, Seccomp, SELinux, or dropping Linux Capabilities are not used to limit unwanted privileges
|
||||
`missingNetworkPolicy` | `warning` | Fails if a controller lacks a corresponding NetworkPolicy
|
||||
`sensitiveConfigmapContent` | `warning` | Fails if a ConfigMap contains a likely password or secret
|
||||
`sensitiveContainerEnvVar` | `warning` | Fails if a controller has an environment variable hard-coded to a likely password or secret
|
||||
|
||||
## Background
|
||||
|
||||
|
||||
Generated
+1891
-1801
File diff suppressed because it is too large
Load Diff
+36
-22
@@ -51,32 +51,46 @@ type ResourceProvider struct {
|
||||
SourceType string
|
||||
Nodes []corev1.Node
|
||||
Namespaces []corev1.Namespace
|
||||
Resources resourceSet
|
||||
Resources resourceKindMap
|
||||
}
|
||||
|
||||
type resourceSet []GenericResource
|
||||
type resourceKindMap map[string][]GenericResource
|
||||
|
||||
func (rs resourceSet) GetNumberOfControllers() int {
|
||||
total := 0
|
||||
func (rkm resourceKindMap) addResource(r GenericResource) {
|
||||
gvk := r.Resource.GroupVersionKind()
|
||||
var key string
|
||||
if gvk.Group != "" {
|
||||
key = gvk.Group + "/" + gvk.Kind
|
||||
} else {
|
||||
key = gvk.Kind
|
||||
}
|
||||
rkm[key] = append(rkm[key], r)
|
||||
}
|
||||
|
||||
func (rkm resourceKindMap) addResources(rs []GenericResource) {
|
||||
for _, r := range rs {
|
||||
if r.PodSpec != nil {
|
||||
total++
|
||||
}
|
||||
rkm.addResource(r)
|
||||
}
|
||||
}
|
||||
|
||||
func (rkm resourceKindMap) GetLength() int {
|
||||
total := 0
|
||||
for _, rs := range rkm {
|
||||
total += len(rs)
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func (rs resourceSet) GetAllOfGroupKind(gk string) []GenericResource {
|
||||
return funk.Filter(rs, func(res GenericResource) bool {
|
||||
gvk := res.Resource.GroupVersionKind()
|
||||
var key string
|
||||
if gvk.Group != "" {
|
||||
key = gvk.Group + "/" + gvk.Kind
|
||||
} else {
|
||||
key = gvk.Kind
|
||||
func (rkm resourceKindMap) GetNumberOfControllers() int {
|
||||
total := 0
|
||||
for _, rs := range rkm {
|
||||
for _, r := range rs {
|
||||
if r.PodSpec != nil {
|
||||
total++
|
||||
}
|
||||
}
|
||||
return key == gk
|
||||
}).([]GenericResource)
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
// This is here for backward compatibility reasons
|
||||
@@ -108,7 +122,7 @@ func newResourceProvider(version, sourceType, sourceName string) ResourceProvide
|
||||
CreationTime: time.Now(),
|
||||
Nodes: make([]corev1.Node, 0),
|
||||
Namespaces: make([]corev1.Namespace, 0),
|
||||
Resources: make([]GenericResource, 0),
|
||||
Resources: make(map[string][]GenericResource),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,7 +193,7 @@ func CreateResourceProviderFromResource(ctx context.Context, workload string) (*
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resources.Resources = append(resources.Resources, workloadObj)
|
||||
resources.Resources.addResource(workloadObj)
|
||||
return &resources, nil
|
||||
}
|
||||
|
||||
@@ -363,7 +377,7 @@ func CreateResourceProviderFromAPI(ctx context.Context, kube kubernetes.Interfac
|
||||
|
||||
provider.Nodes = nodes.Items
|
||||
provider.Namespaces = namespaces.Items
|
||||
provider.Resources = append(provider.Resources, kubernetesResources...)
|
||||
provider.Resources.addResources(kubernetesResources)
|
||||
logrus.Info("Done loading Kubernetes resources")
|
||||
return &provider, nil
|
||||
}
|
||||
@@ -465,13 +479,13 @@ func (resources *ResourceProvider) addResourceFromString(contents string) error
|
||||
return err
|
||||
}
|
||||
workload.OriginalObjectYAML = contentBytes
|
||||
resources.Resources = append(resources.Resources, workload)
|
||||
resources.Resources.addResource(workload)
|
||||
} else {
|
||||
newResource, err := NewGenericResourceFromBytes(contentBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resources.Resources = append(resources.Resources, newResource)
|
||||
resources.Resources.addResource(newResource)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
+14
-18
@@ -21,11 +21,9 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thoas/go-funk"
|
||||
conf "github.com/fairwindsops/polaris/pkg/config"
|
||||
"github.com/fairwindsops/polaris/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
conf "github.com/fairwindsops/polaris/pkg/config"
|
||||
)
|
||||
|
||||
func TestGetResourcesFromPath(t *testing.T) {
|
||||
@@ -44,10 +42,12 @@ func TestGetResourcesFromPath(t *testing.T) {
|
||||
assert.Equal(t, "two", provider.Namespaces[0].ObjectMeta.Name)
|
||||
|
||||
namespaceCount := map[string]int{}
|
||||
for _, controller := range provider.Resources {
|
||||
namespaceCount[controller.ObjectMeta.GetNamespace()]++
|
||||
for _, resources := range provider.Resources {
|
||||
for _, controller := range resources {
|
||||
namespaceCount[controller.ObjectMeta.GetNamespace()]++
|
||||
}
|
||||
}
|
||||
assert.Equal(t, 11, len(provider.Resources))
|
||||
assert.Equal(t, 11, provider.Resources.GetLength())
|
||||
assert.Equal(t, 10, namespaceCount[""])
|
||||
assert.Equal(t, 1, namespaceCount["two"])
|
||||
}
|
||||
@@ -64,11 +64,8 @@ func TestGetMultipleResourceFromSingleFile(t *testing.T) {
|
||||
|
||||
assert.Equal(t, 0, len(resources.Nodes), "Should not have any nodes")
|
||||
|
||||
assert.Equal(t, 6, len(resources.Resources), "Should have 6 resources")
|
||||
deployment := funk.Find(resources.Resources, func(res GenericResource) bool {
|
||||
return res.Resource.GroupVersionKind().Kind == "Deployment"
|
||||
}).(GenericResource)
|
||||
assert.Equal(t, "dashboard", deployment.PodSpec.Containers[0].Name)
|
||||
assert.Equal(t, 1, len(resources.Resources["extensions/Deployment"]), "Should have one controller")
|
||||
assert.Equal(t, "dashboard", resources.Resources["extensions/Deployment"][0].PodSpec.Containers[0].Name)
|
||||
|
||||
assert.Equal(t, 2, len(resources.Namespaces), "Should have a namespace")
|
||||
assert.Equal(t, "polaris", resources.Namespaces[0].ObjectMeta.Name)
|
||||
@@ -90,11 +87,8 @@ func TestAddResourcesFromReader(t *testing.T) {
|
||||
|
||||
assert.Equal(t, 0, len(resources.Nodes), "Should not have any nodes")
|
||||
|
||||
assert.Equal(t, 6, len(resources.Resources), "Should have 6 resources")
|
||||
deployment := funk.Find(resources.Resources, func(res GenericResource) bool {
|
||||
return res.Resource.GroupVersionKind().Kind == "Deployment"
|
||||
}).(GenericResource)
|
||||
assert.Equal(t, "dashboard", deployment.PodSpec.Containers[0].Name)
|
||||
assert.Equal(t, 1, len(resources.Resources["extensions/Deployment"]), "Should have one controller")
|
||||
assert.Equal(t, "dashboard", resources.Resources["extensions/Deployment"][0].PodSpec.Containers[0].Name)
|
||||
|
||||
assert.Equal(t, 2, len(resources.Namespaces), "Should have a namespace")
|
||||
assert.Equal(t, "polaris", resources.Namespaces[0].ObjectMeta.Name)
|
||||
@@ -164,8 +158,10 @@ func TestGetResourceFromAPI(t *testing.T) {
|
||||
assert.Equal(t, 0, len(resources.Nodes), "Should not have any nodes")
|
||||
assert.Equal(t, 5, len(resources.Resources), "Should have 5 controllers")
|
||||
|
||||
for _, ctrl := range resources.Resources {
|
||||
expectedNames[ctrl.ObjectMeta.GetName()] = true
|
||||
for _, controllers := range resources.Resources {
|
||||
for _, ctrl := range controllers {
|
||||
expectedNames[ctrl.ObjectMeta.GetName()] = true
|
||||
}
|
||||
}
|
||||
for name, val := range expectedNames {
|
||||
assert.Equal(t, true, val, name)
|
||||
|
||||
@@ -72,10 +72,7 @@ func TestControllerLevelChecks(t *testing.T) {
|
||||
Severity: "danger",
|
||||
Category: "Reliability",
|
||||
}
|
||||
for _, controller := range res.Resources {
|
||||
if controller.Resource.GroupVersionKind().Kind != "Deployment" {
|
||||
continue
|
||||
}
|
||||
for _, controller := range res.Resources["Deployment"] {
|
||||
actualResult, err := applyControllerSchemaChecks(&c, nil, controller)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -99,7 +96,7 @@ func TestControllerLevelChecks(t *testing.T) {
|
||||
|
||||
res, err := kube.CreateResourceProviderFromPath("../kube/test_files/test_1")
|
||||
assert.Equal(t, nil, err, "Error should be nil")
|
||||
assert.Equal(t, 11, len(res.Resources))
|
||||
assert.Equal(t, 11, res.Resources.GetLength())
|
||||
testResources(res)
|
||||
|
||||
replicaSpec := map[string]interface{}{"replicas": 2}
|
||||
@@ -114,7 +111,7 @@ func TestControllerLevelChecks(t *testing.T) {
|
||||
k8s, dynamicClient := test.SetupTestAPI(&d1, &p1, &d2, &p2)
|
||||
res, err = kube.CreateResourceProviderFromAPI(context.Background(), k8s, "test", dynamicClient, conf.Configuration{})
|
||||
assert.Equal(t, err, nil, "error should be nil")
|
||||
assert.Equal(t, 2, len(res.Resources), "Should have two controllers")
|
||||
assert.Equal(t, 2, res.Resources.GetLength(), "Should have two controllers")
|
||||
testResources(res)
|
||||
}
|
||||
|
||||
|
||||
+10
-2
@@ -173,10 +173,18 @@ func hasExemptionAnnotation(objMeta metaV1.Object, checkID string) bool {
|
||||
|
||||
// ApplyAllSchemaChecksToResourceProvider applies all available checks to a ResourceProvider
|
||||
func ApplyAllSchemaChecksToResourceProvider(conf *config.Configuration, resourceProvider *kube.ResourceProvider) ([]Result, error) {
|
||||
results := []Result{}
|
||||
if resourceProvider == nil {
|
||||
return nil, errors.New("No resource provider set, cannot apply schema checks")
|
||||
}
|
||||
return ApplyAllSchemaChecksToAllResources(conf, resourceProvider, resourceProvider.Resources)
|
||||
for _, resources := range resourceProvider.Resources {
|
||||
kindResults, err := ApplyAllSchemaChecksToAllResources(conf, resourceProvider, resources)
|
||||
if err != nil {
|
||||
return results, err
|
||||
}
|
||||
results = append(results, kindResults...)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// ApplyAllSchemaChecksToAllResources applies available checks to a list of resources
|
||||
@@ -373,7 +381,7 @@ func applySchemaCheck(conf *config.Configuration, checkID string, test schemaTes
|
||||
logrus.Warnf("No ResourceProvider available, check %s will not work in this context (e.g. admission control)", checkID)
|
||||
break
|
||||
}
|
||||
resources := test.ResourceProvider.Resources.GetAllOfGroupKind(groupkind)
|
||||
resources := test.ResourceProvider.Resources[groupkind]
|
||||
namespace := test.Resource.ObjectMeta.GetNamespace()
|
||||
if test.Resource.Kind == "Namespace" {
|
||||
namespace = test.Resource.ObjectMeta.GetName()
|
||||
|
||||
@@ -51,8 +51,9 @@ func TestMutations(t *testing.T) {
|
||||
assert.Len(t, results, 1)
|
||||
allMutations := mutation.GetMutationsFromResults(results)
|
||||
assert.Len(t, allMutations, 1)
|
||||
for _, resource := range tc.resources.Resources {
|
||||
key := fmt.Sprintf("%s/%s/%s", resource.Kind, resource.Resource.GetName(), resource.Resource.GetNamespace())
|
||||
for _, resources := range tc.resources.Resources {
|
||||
assert.Len(t, resources, 1)
|
||||
key := fmt.Sprintf("%s/%s/%s", resources[0].Kind, resources[0].Resource.GetName(), resources[0].Resource.GetNamespace())
|
||||
mutations := allMutations[key]
|
||||
yamlContent, err := mutation.ApplyAllMutations(tc.manifest, mutations)
|
||||
assert.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user