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

feat(core): Move keycloak ers to separate module #1589

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
- lib/ocrypto
- lib/fixtures
- lib/flattening
- keycloak-entity-resolution
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
with:
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ COPY sdk/ sdk/
COPY lib/ocrypto lib/ocrypto
COPY lib/flattening lib/flattening
COPY lib/fixtures lib/fixtures
COPY keycloak-entity-resolution/ keycloak-entity-resolution/
COPY service/ service/
COPY examples/ examples/
COPY go.work go.work.sum ./
Expand Down
3 changes: 2 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.22
go 1.22.0

use (
./examples
Expand All @@ -8,4 +8,5 @@ use (
./protocol/go
./sdk
./service
./keycloak-entity-resolution
)
121 changes: 121 additions & 0 deletions keycloak-entity-resolution/cmd/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package cmd

import (
"context"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"os"
"strings"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
keycloak "github.com/opentdf/platform/keycloak-ers/entityresolution"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"

"github.com/opentdf/platform/protocol/go/entityresolution"
logging "github.com/opentdf/platform/service/logger"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"gopkg.in/yaml.v2"
)

type Config struct {
GRPC GRPCConfig `mapstructure:"grpc" json:"grpc" yaml:"grpc"`
// Port to listen on
Port int `mapstructure:"port" json:"port" yaml:"port" default:"8181"`
Host string `mapstructure:"host,omitempty" json:"host" yaml:"host"`
ERSConfig keycloak.KeycloakConfig `mapstructure:"entityresolution" json:"entityresolution" yaml:"entityresolution"`
Logger logging.Config `mapstructure:"logger" json:"logger" yaml:"logger"`
}

type GRPCConfig struct {
// Enable reflection for grpc server (default: true)
ReflectionEnabled bool `mapstructure:"reflectionEnabled" json:"reflectionEnabled" yaml:"reflectionEnabled" default:"true"`
}

type KeycloakERS struct {
entityresolution.UnimplementedEntityResolutionServiceServer
idpConfig keycloak.KeycloakConfig
logger *logging.Logger
}

func (s KeycloakERS) ResolveEntities(ctx context.Context, req *entityresolution.ResolveEntitiesRequest) (*entityresolution.ResolveEntitiesResponse, error) {
resp, err := keycloak.EntityResolution(ctx, req, s.idpConfig, s.logger)
return &resp, err
}

func (s KeycloakERS) CreateEntityChainFromJwt(ctx context.Context, req *entityresolution.CreateEntityChainFromJwtRequest) (*entityresolution.CreateEntityChainFromJwtResponse, error) {
resp, err := keycloak.CreateEntityChainFromJwt(ctx, req, s.idpConfig, s.logger)
return &resp, err
}

func Execute() {

Check failure on line 55 in keycloak-entity-resolution/cmd/start.go

View workflow job for this annotation

GitHub Actions / go (keycloak-entity-resolution)

unnecessary leading newline (whitespace)

// CONFIGS
var configData Config

f, err := os.Open("config.yaml")
if err != nil {
panic(fmt.Errorf("error when opening YAML file: %s", err.Error()))
}

fileData, err := io.ReadAll(f)
if err != nil {
panic(fmt.Errorf("error reading YAML file: %s", err.Error()))
}

err = yaml.Unmarshal(fileData, &configData)
if err != nil {
panic(fmt.Errorf("error unmarshaling yaml file %s", err.Error()))
}

// SERVER
s := grpc.NewServer()

// Enable grpc reflection
if configData.GRPC.ReflectionEnabled {
reflection.Register(s)
}

logger, err := logging.NewLogger(configData.Logger)
if err != nil {
panic(fmt.Errorf("error creating logger %s", err.Error()))
}

logger.Debug("entity resolution configuration", "config", configData)

svr := KeycloakERS{idpConfig: configData.ERSConfig, logger: logger}

// Register gRPC service
entityresolution.RegisterEntityResolutionServiceServer(s, &svr)

// Create a gRPC-Gateway mux
mux := runtime.NewServeMux()

// Register gRPC Gateway handlers
err = entityresolution.RegisterEntityResolutionServiceHandlerServer(context.Background(), mux, &svr)
if err != nil {
log.Fatalf("failed to register gateway: %v", err)
}

httphandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.TraceContext(r.Context(), "grpc handler func", slog.Int("proto_major", r.ProtoMajor), slog.String("content_type", r.Header.Get("Content-Type")))
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
s.ServeHTTP(w, r)
} else {
mux.ServeHTTP(w, r)
}
})

h2 := h2c.NewHandler(httphandler, &http2.Server{})

// START
logger.Info("Serving gRPC and HTTP on " + fmt.Sprintf("%s:%d", configData.Host, configData.Port))
err = http.ListenAndServe(fmt.Sprintf("%s:%d", configData.Host, configData.Port), h2)

Check failure on line 117 in keycloak-entity-resolution/cmd/start.go

View workflow job for this annotation

GitHub Actions / go (keycloak-entity-resolution)

G114: Use of net/http serve function that has no support for setting timeouts (gosec)
if err != nil {
log.Fatalf("Failed to serve: %v", err)
}
}
17 changes: 17 additions & 0 deletions keycloak-entity-resolution/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
logger:
level: debug
type: text
output: stdout
port: 8181
grpc:
reflectionEnabled: true
entityresolution:
url: http://localhost:8888/auth
clientid: "tdf-entity-resolution"
clientsecret: "secret"
realm: "opentdf"
legacykeycloak: true
inferid:
from:
email: true
username: true
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ const (
const serviceAccountUsernamePrefix = "service-account-"

type KeycloakConfig struct {
URL string `mapstructure:"url" json:"url"`
Realm string `mapstructure:"realm" json:"realm"`
ClientID string `mapstructure:"clientid" json:"clientid"`
ClientSecret string `mapstructure:"clientsecret" json:"clientsecret"`
LegacyKeycloak bool `mapstructure:"legacykeycloak" json:"legacykeycloak" default:"false"`
SubGroups bool `mapstructure:"subgroups" json:"subgroups" default:"false"`
InferID InferredIdentityConfig `mapstructure:"inferid,omitempty" json:"inferid,omitempty"`
URL string `mapstructure:"url" json:"url" yaml:"url"`
Realm string `mapstructure:"realm" json:"realm" yaml:"realm"`
ClientID string `mapstructure:"clientid" json:"clientid" yaml:"clientid"`
ClientSecret string `mapstructure:"clientsecret" json:"clientsecret" yaml:"clientsecret"`
LegacyKeycloak bool `mapstructure:"legacykeycloak" json:"legacykeycloak" yaml:"legacykeycloak" default:"false"`
SubGroups bool `mapstructure:"subgroups" json:"subgroups" yaml:"subgroups" default:"false"`
InferID InferredIdentityConfig `mapstructure:"inferid,omitempty" json:"inferid,omitempty" yaml:"inferid,omitempty"`
}

func (c KeycloakConfig) LogValue() slog.Value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"strings"
"testing"

keycloak "github.com/opentdf/platform/keycloak-ers/entityresolution"
"github.com/opentdf/platform/protocol/go/authorization"
"github.com/opentdf/platform/protocol/go/entityresolution"
keycloak "github.com/opentdf/platform/service/entityresolution/keycloak"
"github.com/opentdf/platform/service/logger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
109 changes: 109 additions & 0 deletions keycloak-entity-resolution/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
module github.com/opentdf/platform/keycloak-ers

go 1.22.0

require (
github.com/Nerzal/gocloak/v13 v13.9.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1
github.com/lestrrat-go/jwx/v2 v2.1.1
github.com/opentdf/platform/protocol/go v0.2.15
github.com/opentdf/platform/service v0.4.23
github.com/stretchr/testify v1.9.0
golang.org/x/net v0.26.0
google.golang.org/grpc v1.66.2
google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v2 v2.4.0
)

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.34.1-20240508200655-46a4cf4ba109.1 // indirect
github.com/Masterminds/squirrel v1.5.4 // indirect
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bmatcuk/doublestar v1.3.4 // indirect
github.com/bufbuild/protovalidate-go v0.6.0 // indirect
github.com/casbin/casbin/v2 v2.84.0 // indirect
github.com/casbin/govaluate v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/creasty/defaults v1.7.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-chi/cors v1.2.1 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/go-resty/resty/v2 v2.12.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/cel-go v0.20.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/gowebpki/jcs v1.0.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
github.com/lestrrat-go/httprc v1.0.6 // indirect
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/open-policy-agent/opa v0.63.0 // indirect
github.com/opentdf/platform/lib/flattening v0.1.1 // indirect
github.com/opentdf/platform/lib/ocrypto v0.1.5 // indirect
github.com/opentdf/platform/sdk v0.3.12 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/pressly/goose/v3 v3.19.1 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.6.0 // indirect
github.com/prometheus/common v0.52.3 // indirect
github.com/prometheus/procfs v0.13.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/segmentio/ksuid v1.0.4 // indirect
github.com/sethvargo/go-retry v0.2.4 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/valyala/fasthttp v1.52.0 // indirect
github.com/wI2L/jsondiff v0.5.2 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/yashtewari/glob-intersection v0.2.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading
Loading