Skip to content

Commit 37e7d38

Browse files
authored
Adds check for SPIFFEID of client (#125)
1 parent 72b6db1 commit 37e7d38

File tree

1 file changed

+24
-1
lines changed
  • workloads/ping-pong/ping-pong-server

1 file changed

+24
-1
lines changed

workloads/ping-pong/ping-pong-server/main.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/prometheus/client_golang/prometheus"
1313
"github.com/prometheus/client_golang/prometheus/promauto"
1414
"github.com/prometheus/client_golang/prometheus/promhttp"
15+
"github.com/spiffe/go-spiffe/v2/spiffeid"
1516
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
1617
"github.com/spiffe/go-spiffe/v2/workloadapi"
1718
)
@@ -70,6 +71,18 @@ type Env struct {
7071
MetricsPort string
7172
SpiffeSocketPath string
7273
MetricsEnabled bool
74+
// ClientSPIFFEID is the expected SPIFFEID of the
75+
// client making inbound requests to this server
76+
ClientSPIFFEID string
77+
}
78+
79+
func mustGetEnv(variable string) string {
80+
v, ok := os.LookupEnv(variable)
81+
if !ok {
82+
slog.Error("Unset environment variable", "variable", variable)
83+
os.Exit(1)
84+
}
85+
return v
7386
}
7487

7588
func getEnvWithDefault(variable string, defaultValue string) string {
@@ -99,6 +112,7 @@ func getEnv() *Env {
99112
MetricsPort: getEnvWithDefault("METRICS_PORT", ":8080"),
100113
SpiffeSocketPath: getEnvWithDefault("SPIFFE_ENDPOINT_SOCKET", "unix:///spiffe-workload-api/spire-agent.sock"),
101114
MetricsEnabled: getEnvBooleanWithDefault("METRICS_ENABLED", true),
115+
ClientSPIFFEID: mustGetEnv("CLIENT_SPIFFE_ID"),
102116
}
103117
}
104118

@@ -132,7 +146,16 @@ func run(ctx context.Context, env *Env) error {
132146
svidURISAN.WithLabelValues(svid.ID.String()).Set(1)
133147
}
134148

135-
tlsConfig := tlsconfig.MTLSServerConfig(source, source, tlsconfig.AuthorizeAny())
149+
// Only authorize inbound calls from the expected client SPIFFE IDs
150+
clientSPIFFEID, err := spiffeid.FromString(env.ClientSPIFFEID)
151+
if err != nil {
152+
return fmt.Errorf("failed to parse client SPIFFE ID: %w", err)
153+
}
154+
tlsConfig := tlsconfig.MTLSServerConfig(
155+
source,
156+
source,
157+
tlsconfig.AuthorizeID(clientSPIFFEID),
158+
)
136159
server := &http.Server{
137160
Addr: env.Port,
138161
TLSConfig: tlsConfig,

0 commit comments

Comments
 (0)