Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for LocalDataSource #18

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions internal/pkg/test/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ var trustZoneFixtures map[string]*trust_zone_proto.TrustZone = map[string]*trust
"tz1": {
Name: "tz1",
TrustDomain: "td1",
KubernetesCluster: stringPtr("local1"),
KubernetesContext: stringPtr("kind-local1"),
KubernetesCluster: StringPtr("local1"),
KubernetesContext: StringPtr("kind-local1"),
TrustProvider: &trust_provider_proto.TrustProvider{
Kind: stringPtr("kubernetes"),
Kind: StringPtr("kubernetes"),
},
BundleEndpointUrl: stringPtr("127.0.0.1"),
BundleEndpointUrl: StringPtr("127.0.0.1"),
Federations: []*federation_proto.Federation{
{
From: "tz1",
Expand All @@ -42,12 +42,12 @@ var trustZoneFixtures map[string]*trust_zone_proto.TrustZone = map[string]*trust
"tz2": {
Name: "tz2",
TrustDomain: "td2",
KubernetesCluster: stringPtr("local2"),
KubernetesContext: stringPtr("kind-local2"),
KubernetesCluster: StringPtr("local2"),
KubernetesContext: StringPtr("kind-local2"),
TrustProvider: &trust_provider_proto.TrustProvider{
Kind: stringPtr("kubernetes"),
Kind: StringPtr("kubernetes"),
},
BundleEndpointUrl: stringPtr("127.0.0.2"),
BundleEndpointUrl: StringPtr("127.0.0.2"),
Federations: []*federation_proto.Federation{
{
From: "tz2",
Expand All @@ -62,6 +62,19 @@ var trustZoneFixtures map[string]*trust_zone_proto.TrustZone = map[string]*trust
},
},
},
// tz3 has no federations or bound attestation policies.
"tz3": {
Name: "tz3",
TrustDomain: "td3",
KubernetesCluster: StringPtr("local3"),
KubernetesContext: StringPtr("kind-local3"),
TrustProvider: &trust_provider_proto.TrustProvider{
Kind: StringPtr("kubernetes"),
},
BundleEndpointUrl: StringPtr("127.0.0.3"),
Federations: []*federation_proto.Federation{},
AttestationPolicies: []*ap_binding_proto.APBinding{},
},
}

var attestationPolicyFixtures map[string]*attestation_policy_proto.AttestationPolicy = map[string]*attestation_policy_proto.AttestationPolicy{
Expand Down Expand Up @@ -150,6 +163,6 @@ func AttestationPolicy(name string) *attestation_policy_proto.AttestationPolicy
return ap
}

func stringPtr(s string) *string {
func StringPtr(s string) *string {
return &s
}
54 changes: 27 additions & 27 deletions pkg/plugin/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ func validateTrustProviderUpdate(tzName string, current, new *trust_provider_pro
return nil
}

func (lds *LocalDataSource) ListTrustZones() ([]*trust_zone_proto.TrustZone, error) {
trustZones := []*trust_zone_proto.TrustZone{}
for _, trustZone := range lds.config.TrustZones {
trustZone, err := proto.CloneTrustZone(trustZone)
if err != nil {
return nil, err
}
trustZones = append(trustZones, trustZone)
}
return trustZones, nil
}

func (lds *LocalDataSource) AddAttestationPolicy(policy *attestation_policy_proto.AttestationPolicy) (*attestation_policy_proto.AttestationPolicy, error) {
if _, ok := lds.config.GetAttestationPolicyByName(policy.Name); ok {
return nil, fmt.Errorf("attestation policy %s already exists in local config", policy.Name)
Expand All @@ -169,6 +181,18 @@ func (lds *LocalDataSource) GetAttestationPolicy(id string) (*attestation_policy
}
}

func (lds *LocalDataSource) ListAttestationPolicies() ([]*attestation_policy_proto.AttestationPolicy, error) {
policies := []*attestation_policy_proto.AttestationPolicy{}
for _, policy := range lds.config.AttestationPolicies {
policy, err := proto.CloneAttestationPolicy(policy)
if err != nil {
return nil, err
}
policies = append(policies, policy)
}
return policies, nil
}

func (lds *LocalDataSource) AddAPBinding(binding *ap_binding_proto.APBinding) (*ap_binding_proto.APBinding, error) {
localTrustZone, ok := lds.config.GetTrustZoneByName(binding.TrustZone)
if !ok {
Expand All @@ -177,7 +201,7 @@ func (lds *LocalDataSource) AddAPBinding(binding *ap_binding_proto.APBinding) (*

_, ok = lds.config.GetAttestationPolicyByName(binding.Policy)
if !ok {
return nil, fmt.Errorf("attestation policy %s does not exist in local config", binding.Policy)
return nil, fmt.Errorf("failed to find attestation policy %s in local config", binding.Policy)
}

for _, apb := range localTrustZone.AttestationPolicies {
Expand Down Expand Up @@ -285,33 +309,9 @@ func (lds *LocalDataSource) AddFederation(federationProto *federation_proto.Fede
return proto.CloneFederation(federationProto)
}

func (lds *LocalDataSource) ListTrustZones() ([]*trust_zone_proto.TrustZone, error) {
trustZones := []*trust_zone_proto.TrustZone{}
for _, trustZone := range lds.config.TrustZones {
trustZone, err := proto.CloneTrustZone(trustZone)
if err != nil {
return nil, err
}
trustZones = append(trustZones, trustZone)
}
return trustZones, nil
}

func (lds *LocalDataSource) ListAttestationPolicies() ([]*attestation_policy_proto.AttestationPolicy, error) {
var policies []*attestation_policy_proto.AttestationPolicy
for _, policy := range lds.config.AttestationPolicies {
policy, err := proto.CloneAttestationPolicy(policy)
if err != nil {
return nil, err
}
policies = append(policies, policy)
}
return policies, nil
}

func (lds *LocalDataSource) ListFederations() ([]*federation_proto.Federation, error) {
// federations are expressed in-line with the trust zone(s) so we need to iterate the trust zones
var federations []*federation_proto.Federation
federations := []*federation_proto.Federation{}
for _, trustZone := range lds.config.TrustZones {
for _, federation := range trustZone.Federations {
federation, err := proto.CloneFederation(federation)
Expand All @@ -327,7 +327,7 @@ func (lds *LocalDataSource) ListFederations() ([]*federation_proto.Federation, e
func (lds *LocalDataSource) ListFederationsByTrustZone(tzName string) ([]*federation_proto.Federation, error) {
trustZone, ok := lds.config.GetTrustZoneByName(tzName)
if !ok {
return nil, fmt.Errorf("failed to find trust zone %s in local config", trustZone.Name)
return nil, fmt.Errorf("failed to find trust zone %s in local config", tzName)
}

var federations []*federation_proto.Federation
Expand Down
Loading
Loading