Skip to content

Commit

Permalink
Add Peer field to Intention CRD source intention (#1263)
Browse files Browse the repository at this point in the history
update source intentions with peer name and bump api module and change initiate -> establish
  • Loading branch information
ndhanushkodi committed Jun 13, 2022
1 parent 8c0978f commit a9637a9
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 218 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ jobs:
working-directory: control-plane
run: |
mkdir -p $HOME/bin
wget https://github.com/ndhanushkodi/binaries/releases/download/v2oss/consul -O consulbin && \
wget https://github.com/ndhanushkodi/binaries/releases/download/v3oss/consul -O consulbin && \
mv consulbin $HOME/bin/consul &&
chmod +x $HOME/bin/consul
Expand Down Expand Up @@ -194,7 +194,7 @@ jobs:
working-directory: control-plane
run: |
mkdir -p $HOME/bin
wget https://github.com/ndhanushkodi/binaries/releases/download/v2ent/consul -O consulbin && \
wget https://github.com/ndhanushkodi/binaries/releases/download/v3ent/consul -O consulbin && \
mv consulbin $HOME/bin/consul &&
chmod +x $HOME/bin/consul
Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/crd-serviceintentions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ spec:
partition:
description: Partition is the Admin Partition for the Name parameter.
type: string
peer:
description: Peer is the peer name for the Name parameter.
type: string
permissions:
description: Permissions is the list of all additional L7 attributes
that extend the intention match criteria. Permission precedence
Expand Down
121 changes: 0 additions & 121 deletions charts/consul/test/terraform/eks/.terraform.lock.hcl

This file was deleted.

77 changes: 0 additions & 77 deletions charts/consul/test/terraform/gke/.terraform.lock.hcl

This file was deleted.

19 changes: 12 additions & 7 deletions control-plane/api/v1alpha1/serviceintentions_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ type SourceIntention struct {
Name string `json:"name,omitempty"`
// Namespace is the namespace for the Name parameter.
Namespace string `json:"namespace,omitempty"`
// Peer is the peer name for the Name parameter.
Peer string `json:"peer,omitempty"`
// Partition is the Admin Partition for the Name parameter.
Partition string `json:"partition,omitempty"`
// Action is required for an L4 intention, and should be set to one of
Expand Down Expand Up @@ -270,7 +272,7 @@ func (in *ServiceIntentions) Validate(consulMeta common.ConsulMeta) error {
}

errs = append(errs, in.validateNamespaces(consulMeta.NamespacesEnabled)...)
errs = append(errs, in.validatePartitions(consulMeta.PartitionsEnabled)...)
errs = append(errs, in.validateSourcePeerAndPartitions(consulMeta.PartitionsEnabled)...)

if len(errs) > 0 {
return apierrors.NewInvalid(
Expand Down Expand Up @@ -311,6 +313,7 @@ func (in *SourceIntention) toConsul() *capi.SourceIntention {
Name: in.Name,
Namespace: in.Namespace,
Partition: in.Partition,
Peer: in.Peer,
Action: in.Action.toConsul(),
Permissions: in.Permissions.toConsul(),
Description: in.Description,
Expand Down Expand Up @@ -455,14 +458,16 @@ func (in *ServiceIntentions) validateNamespaces(namespacesEnabled bool) field.Er
return errs
}

func (in *ServiceIntentions) validatePartitions(partitionsEnabled bool) field.ErrorList {
func (in *ServiceIntentions) validateSourcePeerAndPartitions(partitionsEnabled bool) field.ErrorList {
var errs field.ErrorList
path := field.NewPath("spec")
if !partitionsEnabled {
for i, source := range in.Spec.Sources {
if source.Partition != "" {
errs = append(errs, field.Invalid(path.Child("sources").Index(i).Child("partition"), source.Partition, `Consul Enterprise Admin Partitions must be enabled to set source.partition`))
}
for i, source := range in.Spec.Sources {
if source.Partition != "" && !partitionsEnabled {
errs = append(errs, field.Invalid(path.Child("sources").Index(i).Child("partition"), source.Partition, `Consul Enterprise Admin Partitions must be enabled to set source.partition`))
}

if source.Peer != "" && source.Partition != "" {
errs = append(errs, field.Invalid(path.Child("sources").Index(i), source, `Both source.peer and source.partition cannot be set.`))
}
}
return errs
Expand Down
67 changes: 67 additions & 0 deletions control-plane/api/v1alpha1/serviceintentions_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,73 @@ func TestServiceIntentions_Validate(t *testing.T) {
`spec.sources[2].partition: Invalid value: "partition-foo": Consul Enterprise Admin Partitions must be enabled to set source.partition`,
},
},
"single source peer and partition specified": {
input: &ServiceIntentions{
ObjectMeta: metav1.ObjectMeta{
Name: "does-not-matter",
},
Spec: ServiceIntentionsSpec{
Destination: Destination{
Name: "dest-service",
Namespace: "namespace-a",
},
Sources: SourceIntentions{
{
Name: "web",
Action: "allow",
Namespace: "namespace-b",
Partition: "partition-other",
Peer: "peer-other",
},
{
Name: "db",
Action: "deny",
Namespace: "namespace-c",
},
},
},
},
namespacesEnabled: true,
partitionsEnabled: true,
expectedErrMsgs: []string{
`spec.sources[0]: Invalid value: v1alpha1.SourceIntention{Name:"web", Namespace:"namespace-b", Peer:"peer-other", Partition:"partition-other", Action:"allow", Permissions:v1alpha1.IntentionPermissions(nil), Description:""}: Both source.peer and source.partition cannot be set.`,
},
},
"multiple source peer and partition specified": {
input: &ServiceIntentions{
ObjectMeta: metav1.ObjectMeta{
Name: "does-not-matter",
},
Spec: ServiceIntentionsSpec{
Destination: Destination{
Name: "dest-service",
Namespace: "namespace-a",
},
Sources: SourceIntentions{
{
Name: "web",
Action: "allow",
Namespace: "namespace-b",
Partition: "partition-other",
Peer: "peer-other",
},
{
Name: "db",
Action: "deny",
Namespace: "namespace-c",
Partition: "partition-2",
Peer: "peer-2",
},
},
},
},
namespacesEnabled: true,
partitionsEnabled: true,
expectedErrMsgs: []string{
`spec.sources[0]: Invalid value: v1alpha1.SourceIntention{Name:"web", Namespace:"namespace-b", Peer:"peer-other", Partition:"partition-other", Action:"allow", Permissions:v1alpha1.IntentionPermissions(nil), Description:""}: Both source.peer and source.partition cannot be set.`,
`spec.sources[1]: Invalid value: v1alpha1.SourceIntention{Name:"db", Namespace:"namespace-c", Peer:"peer-2", Partition:"partition-2", Action:"deny", Permissions:v1alpha1.IntentionPermissions(nil), Description:""}: Both source.peer and source.partition cannot be set.`,
},
},
}
for name, testCase := range cases {
t.Run(name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ spec:
partition:
description: Partition is the Admin Partition for the Name parameter.
type: string
peer:
description: Peer is the peer name for the Name parameter.
type: string
permissions:
description: Permissions is the list of all additional L7 attributes
that extend the intention match criteria. Permission precedence
Expand Down
Loading

0 comments on commit a9637a9

Please sign in to comment.