Skip to content

Commit fdbbed1

Browse files
authored
Adds Reason column to federation list output (#118)
* Adds Reason column to federation list, integration test coverage for no bundle case * [From review] Adjust consts
1 parent 90d2631 commit fdbbed1

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

cmd/cofidectl/cmd/federation/federation.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ import (
1818
"github.com/spf13/cobra"
1919
)
2020

21+
const (
22+
FederationStatusHealthy string = "Healthy"
23+
FederationStatusUnhealthy string = "Unhealthy"
24+
25+
FederationStatusReasonNoBundleFound string = "No bundle found"
26+
FederationStatusReasonBundlesDoNotMatch string = "Bundles do not match"
27+
)
28+
2129
type FederationCommand struct {
2230
cmdCtx *cmdcontext.CommandContext
2331
}
@@ -84,7 +92,7 @@ func (c *FederationCommand) GetListCommand() *cobra.Command {
8492
return err
8593
}
8694

87-
status, err := checkFederationStatus(cmd.Context(), kubeConfig, from, to)
95+
status, reason, err := checkFederationStatus(cmd.Context(), kubeConfig, from, to)
8896
if err != nil {
8997
return err
9098
}
@@ -93,11 +101,12 @@ func (c *FederationCommand) GetListCommand() *cobra.Command {
93101
federation.From,
94102
federation.To,
95103
status,
104+
reason,
96105
}
97106
}
98107

99108
table := tablewriter.NewWriter(os.Stdout)
100-
table.SetHeader([]string{"From Trust Zone", "To Trust Zone", "Status"})
109+
table.SetHeader([]string{"From Trust Zone", "To Trust Zone", "Status", "Reason"})
101110
table.SetBorder(false)
102111
table.AppendBulk(data)
103112
table.Render()
@@ -115,24 +124,24 @@ type bundles struct {
115124

116125
// checkFederationStatus builds a comparison map between two trust domains, retrieves there server CA bundle and any federated bundles available
117126
// locally from the SPIRE server, and then compares the bundles on each to verify SPIRE has the correct bundles on each side of the federation
118-
func checkFederationStatus(ctx context.Context, kubeConfig string, from *trust_zone_proto.TrustZone, to *trust_zone_proto.TrustZone) (string, error) {
127+
func checkFederationStatus(ctx context.Context, kubeConfig string, from *trust_zone_proto.TrustZone, to *trust_zone_proto.TrustZone) (string, string, error) {
119128
compare := make(map[*trust_zone_proto.TrustZone]bundles)
120129

121130
for _, tz := range []*trust_zone_proto.TrustZone{from, to} {
122131
if deployed, err := isTrustZoneDeployed(ctx, tz); err != nil {
123-
return "", err
132+
return "", "", err
124133
} else if !deployed {
125-
return "Inactive", nil
134+
return "Inactive", "", nil
126135
}
127136

128137
client, err := kubeutil.NewKubeClientFromSpecifiedContext(kubeConfig, tz.GetKubernetesContext())
129138
if err != nil {
130-
return "", err
139+
return "", "", err
131140
}
132141

133142
serverCABundle, federatedBundles, err := spire.GetServerCABundleAndFederatedBundles(ctx, client)
134143
if err != nil {
135-
return "", err
144+
return "", "", err
136145
}
137146

138147
compare[tz] = bundles{
@@ -144,15 +153,15 @@ func checkFederationStatus(ctx context.Context, kubeConfig string, from *trust_z
144153
// Bundle does not exist at all on opposite trust domain
145154
_, ok := compare[from].federatedBundles[to.TrustDomain]
146155
if !ok {
147-
return "Unhealthy", nil
156+
return FederationStatusUnhealthy, FederationStatusReasonNoBundleFound, nil
148157
}
149158

150159
// Bundle does not match entry on opposite trust domain
151160
if compare[from].federatedBundles[to.TrustDomain] != compare[to].serverCABundle {
152-
return "Unhealthy", nil
161+
return FederationStatusUnhealthy, FederationStatusReasonBundlesDoNotMatch, nil
153162
}
154163

155-
return "Healthy", nil
164+
return FederationStatusHealthy, "", nil
156165
}
157166

158167
// isTrustZoneDeployed returns whether a trust zone has been deployed, i.e. whether a SPIRE Helm release has been installed.

tests/integration/federation/test.sh

+11-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,16 @@ function show_workload_status() {
127127
fi
128128

129129
echo "cofidectl workload status successful"
130-
exit 0
130+
}
131+
132+
function teardown_federation_and_verify() {
133+
kubectl --context $K8S_CLUSTER_2_CONTEXT delete clusterspiffeids.spire.spiffe.io spire-spire-namespace
134+
kubectl exec --context $K8S_CLUSTER_2_CONTEXT -n spire spire-server-0 -- /opt/spire/bin/spire-server federation delete -id td1
135+
kubectl exec --context $K8S_CLUSTER_2_CONTEXT -n spire spire-server-0 -- /opt/spire/bin/spire-server bundle delete -id td1
136+
federations=$(./cofidectl federation list)
137+
if ! echo "$federations" | grep "Unhealthy | No bundle found" >/dev/null; then
138+
return 1
139+
fi
131140
}
132141

133142
function down() {
@@ -145,6 +154,7 @@ function main() {
145154
run_tests
146155
post_deploy
147156
show_workload_status
157+
teardown_federation_and_verify
148158
down
149159
echo "Success!"
150160
}

tests/integration/single-trust-zone/test.sh

-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ function show_workload_status() {
9595
fi
9696

9797
echo "cofidectl workload status successful"
98-
exit 0
9998
}
10099

101100
function down() {

0 commit comments

Comments
 (0)