Skip to content

Commit

Permalink
Implement TraT toggle in stocks service (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
kchiranjewee63 authored Sep 3, 2024
1 parent 78624e3 commit 3ed6635
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions deploy/alpha-stocks-dev/deployments/stocks-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ spec:
value: spiffe://dev.alphastocks.com/stocks
- name: TRAT_VERIFY_ENDPOINT
value: http://localhost:9030/verify-trat
- name: ENABLE_TRATS
value: "${ENABLE_TRATS}"
image: ghcr.io/tratteria/example-application/stocks:latest
name: stocks
ports:
Expand Down
17 changes: 17 additions & 0 deletions stocks/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"os"
"strconv"
"time"

"github.com/spiffe/go-spiffe/v2/spiffeid"
Expand All @@ -22,6 +23,7 @@ type spiffeIDs struct {
type StocksConfig struct {
SpiffeIDs *spiffeIDs
TratVerifyEndpoint *url.URL
EnableTrats bool
}

func GetAppConfig() *StocksConfig {
Expand All @@ -32,6 +34,7 @@ func GetAppConfig() *StocksConfig {
Stocks: spiffeid.RequireFromString(getEnv("STOCKS_SERVICE_SPIFFE_ID")),
},
TratVerifyEndpoint: parseURL(getEnv("TRAT_VERIFY_ENDPOINT")),
EnableTrats: getBoolFromEnv("ENABLE_TRATS"),
}
}

Expand Down Expand Up @@ -64,3 +67,17 @@ func parseURL(rawurl string) *url.URL {

return parsedURL
}

func getBoolFromEnv(key string) bool {
value, exists := os.LookupEnv(key)
if !exists {
panic(fmt.Sprintf("%s environment variable not set", key))
}

parsed, err := strconv.ParseBool(value)
if err != nil {
panic(fmt.Sprintf("Invalid boolean value for %s: %s", key, value))
}

return parsed
}
4 changes: 3 additions & 1 deletion stocks/pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func GetMiddleware(stocksConfig *config.StocksConfig, spireJwtSource *workloadap

middlewareList = append(middlewareList, getSpiffeMiddleware(stocksConfig, spireJwtSource, logger))

middlewareList = append(middlewareList, getTraTVerifierMiddleware(stocksConfig.TratVerifyEndpoint, httpClient, logger))
if stocksConfig.EnableTrats {
middlewareList = append(middlewareList, getTraTVerifierMiddleware(stocksConfig.TratVerifyEndpoint, httpClient, logger))
}

return CombineMiddleware(middlewareList...)
}

0 comments on commit 3ed6635

Please sign in to comment.