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

fixing the lookup for preshared key #18

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ FROM registry.access.redhat.com/ubi9/ubi-minimal:9.3
COPY --from=builder /workspace/bin/ciam-rebac /usr/local/bin/
COPY --from=builder /workspace/configs/config.yaml /usr/local/bin/

ENV SPICEDB_PRESHARED $SPICEDB_PRESHARED
EXPOSE 8000
EXPOSE 9000

Expand Down
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ kind/spicedb:

.PHONY: run
# run api locally
run:
./bin/ciam-rebac -conf configs

run: build
./bin/ciam-rebac -conf configs
# show help
help:
@echo ''
Expand Down
10 changes: 10 additions & 0 deletions cmd/ciam-rebac/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"github.com/go-kratos/kratos/v2/config/env"
"os"

"ciam-rebac/internal/conf"
Expand Down Expand Up @@ -60,6 +61,7 @@ func main() {
)
c := config.New(
config.WithSource(
env.NewSource("SPICEDB_"),
file.NewSource(flagconf),
),
)
Expand All @@ -74,6 +76,14 @@ func main() {
panic(err)
}

preshared, err := c.Value("PRESHARED").String()
if err != nil {
log.NewHelper(logger).Errorf("Failed to read preshared key env %d", err)
}
if preshared != "" {
bc.Data.SpiceDb.Token = preshared
}

app, cleanup, err := wireApp(bc.Server, bc.Data, logger)
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion configs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ data:
spiceDb:
useTLS: false
endpoint: spicedb:50051
tokenFile: /spicedb_pre_shared
token: "${SPICEDB_PRESHARED:foobar}"
4 changes: 2 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ version: "3.8"
services:
rebac:
image: "quay.io/ciam_authz/insights-rebac:latest"
configs:
- spicedb_pre_shared
environment:
- "SPICEDB_PRESHARED=${SPICEDB_GRPC_PRESHARED_KEY}"
build:
dockerfile: Dockerfile
profiles: ["rebac"]
Expand Down
26 changes: 13 additions & 13 deletions internal/conf/conf.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/conf/conf.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ message Data {
message SpiceDb {
bool useTLS = 1;
string endpoint = 2;
string tokenFile = 3;
string token = 3;
}
SpiceDb spiceDb = 1;
}
16 changes: 3 additions & 13 deletions internal/data/spicedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/go-kratos/kratos/v2/log"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"os"
)

// SpiceDbRepository .
Expand All @@ -27,9 +26,9 @@ func NewSpiceDbRepository(c *conf.Data, logger log.Logger) (*SpiceDbRepository,
var opts []grpc.DialOption
opts = append(opts, grpc.WithBlock()) // TODO: always did it this way with authz. Still the right option?

token, err := readToken(c.SpiceDb.TokenFile)
if err != nil {
err = fmt.Errorf("error extracting token from file: %w", err)
token := c.SpiceDb.Token
if token == "" {
err := fmt.Errorf("token is empty: %s", token)
log.NewHelper(logger).Error(err)
return nil, nil, err
}
Expand Down Expand Up @@ -134,12 +133,3 @@ func createSpiceDbRelationship(relationship *apiV1.Relationship) *v1.Relationshi
Subject: subject,
}
}

func readToken(file string) (string, error) {
bytes, err := os.ReadFile(file)
if err != nil {
return "", err
}

return string(bytes), nil
}
Loading