-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3420 from deads2k/secret-sa-check
Merged by openshift-bot
- Loading branch information
Showing
13 changed files
with
555 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package analysis | ||
|
||
import ( | ||
osgraph "github.com/openshift/origin/pkg/api/graph" | ||
kubeedges "github.com/openshift/origin/pkg/api/kubegraph" | ||
kubegraph "github.com/openshift/origin/pkg/api/kubegraph/nodes" | ||
) | ||
|
||
// CheckMountedSecrets checks to be sure that all the referenced secrets are mountable (by service account) and present (not synthetic) | ||
func CheckMountedSecrets(g osgraph.Graph, podSpecNode *kubegraph.PodSpecNode) ( /*unmountable secrets*/ []*kubegraph.SecretNode /*unresolved secrets*/, []*kubegraph.SecretNode) { | ||
saNodes := g.SuccessorNodesByNodeAndEdgeKind(podSpecNode, kubegraph.ServiceAccountNodeKind, kubeedges.ReferencedServiceAccountEdgeKind) | ||
saMountableSecrets := []*kubegraph.SecretNode{} | ||
|
||
if len(saNodes) > 0 { | ||
saNode := saNodes[0].(*kubegraph.ServiceAccountNode) | ||
for _, secretNode := range g.SuccessorNodesByNodeAndEdgeKind(saNode, kubegraph.SecretNodeKind, kubeedges.MountableSecretEdgeKind) { | ||
saMountableSecrets = append(saMountableSecrets, secretNode.(*kubegraph.SecretNode)) | ||
} | ||
} | ||
|
||
unmountableSecrets := []*kubegraph.SecretNode{} | ||
missingSecrets := []*kubegraph.SecretNode{} | ||
|
||
for _, uncastMountedSecretNode := range g.SuccessorNodesByNodeAndEdgeKind(podSpecNode, kubegraph.SecretNodeKind, kubeedges.MountedSecretEdgeKind) { | ||
mountedSecretNode := uncastMountedSecretNode.(*kubegraph.SecretNode) | ||
if !mountedSecretNode.Found() { | ||
missingSecrets = append(missingSecrets, mountedSecretNode) | ||
} | ||
|
||
mountable := false | ||
for _, mountableSecretNode := range saMountableSecrets { | ||
if mountableSecretNode == mountedSecretNode { | ||
mountable = true | ||
break | ||
} | ||
} | ||
|
||
if !mountable { | ||
unmountableSecrets = append(unmountableSecrets, mountedSecretNode) | ||
continue | ||
} | ||
} | ||
|
||
return unmountableSecrets, missingSecrets | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package kubegraph | ||
|
||
import ( | ||
"testing" | ||
|
||
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api" | ||
|
||
osgraph "github.com/openshift/origin/pkg/api/graph" | ||
kubegraph "github.com/openshift/origin/pkg/api/kubegraph/nodes" | ||
) | ||
|
||
func TestSecretEdges(t *testing.T) { | ||
sa := &kapi.ServiceAccount{} | ||
sa.Namespace = "ns" | ||
sa.Name = "shultz" | ||
sa.Secrets = []kapi.ObjectReference{{Name: "i-know-nothing"}, {Name: "missing"}} | ||
|
||
secret1 := &kapi.Secret{} | ||
secret1.Namespace = "ns" | ||
secret1.Name = "i-know-nothing" | ||
|
||
pod := &kapi.Pod{} | ||
pod.Namespace = "ns" | ||
pod.Name = "the-pod" | ||
pod.Spec.Volumes = []kapi.Volume{{Name: "rose", VolumeSource: kapi.VolumeSource{Secret: &kapi.SecretVolumeSource{SecretName: "i-know-nothing"}}}} | ||
|
||
g := osgraph.New() | ||
saNode := kubegraph.EnsureServiceAccountNode(g, sa) | ||
secretNode := kubegraph.EnsureSecretNode(g, secret1) | ||
podNode := kubegraph.EnsurePodNode(g, pod) | ||
|
||
AddAllMountableSecretEdges(g) | ||
AddAllMountedSecretEdges(g) | ||
|
||
if edge := g.EdgeBetween(saNode, secretNode); edge == nil { | ||
t.Errorf("edge missing") | ||
} else { | ||
if edgeKind := g.EdgeKind(edge); edgeKind != MountableSecretEdgeKind { | ||
t.Errorf("expected %v, got %v", MountableSecretEdgeKind, edgeKind) | ||
} | ||
} | ||
|
||
podSpecNodes := g.SuccessorNodesByNodeAndEdgeKind(podNode, kubegraph.PodSpecNodeKind, osgraph.ContainsEdgeKind) | ||
if len(podSpecNodes) != 1 { | ||
t.Fatalf("wrong number of podspecs: %v", podSpecNodes) | ||
} | ||
|
||
if edge := g.EdgeBetween(podSpecNodes[0], secretNode); edge == nil { | ||
t.Errorf("edge missing") | ||
} else { | ||
if edgeKind := g.EdgeKind(edge); edgeKind != MountedSecretEdgeKind { | ||
t.Errorf("expected %v, got %v", MountedSecretEdgeKind, edgeKind) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.