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: OBS-405 - reconciler svc: add API keys #42

Merged
merged 3 commits into from
Feb 19, 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
2 changes: 1 addition & 1 deletion diode-server/cmd/reconciler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func main() {
ctx := context.Background()
s := server.New(ctx, "diode-reconciler")

reconcilerComponent, err := reconciler.New(s.Logger())
reconcilerComponent, err := reconciler.New(ctx, s.Logger())
if err != nil {
s.Logger().Error("failed to instantiate reconciler component", "error", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion diode-server/distributor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ type Config struct {
RedisHost string `envconfig:"REDIS_HOST" default:"127.0.0.1"`
RedisPort string `envconfig:"REDIS_PORT" default:"6379"`
RedisPassword string `envconfig:"REDIS_PASSWORD" required:"true"`
RedisStreamDB int `envconfig:"REDIS_STREAM_DB" default:"0"`
RedisStreamDB int `envconfig:"REDIS_STREAM_DB" default:"1"`
}
5 changes: 4 additions & 1 deletion diode-server/docker/diode/env/reconciler.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
API_KEY=CHANGE_.ME
REDIS_PASSWORD=@FmnLoA*VnebyVnZoL.!-.6z
REDIS_HOST=diode-redis
REDIS_PORT=6379
DIODE_TO_NETBOX_API_KEY=1368dbad13e418d5a443d93cf255edde03a2a754
NETBOX_DIODE_API_KEY=1e99338b8cab5fc637bc55f390bda1446f619c42
DATASOURCE_TO_DIODE_API_KEY=5a52c45ee8231156cb620d193b0291912dd15433
NETBOX_API_URL=http://netbox:8000/api
2 changes: 1 addition & 1 deletion diode-server/ingester/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ type Config struct {
RedisHost string `envconfig:"REDIS_HOST" default:"127.0.0.1"`
RedisPort string `envconfig:"REDIS_PORT" default:"6379"`
RedisPassword string `envconfig:"REDIS_PASSWORD" required:"true"`
RedisStreamDB int `envconfig:"REDIS_STREAM_DB" default:"0"`
RedisStreamDB int `envconfig:"REDIS_STREAM_DB" default:"1"`
RedisDB int `envconfig:"REDIS_DB" default:"0"`
}
29 changes: 29 additions & 0 deletions diode-server/reconciler/api_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package reconciler

import (
"context"
"encoding/json"
"github.com/redis/go-redis/v9"
)

// APIKeys is a map of API keys
type APIKeys map[string]string

// MarshalBinary marshals APIKeys to JSON encoding
func (ak APIKeys) MarshalBinary() ([]byte, error) {
return json.Marshal(ak)
}

func loadAPIKeys(ctx context.Context, cfg Config, rc *redis.Client) (APIKeys, error) {
apiKeys := map[string]string{
"DIODE_TO_NETBOX": cfg.DiodeToNetBoxAPIKey,
"NETBOX_TO_DIODE": cfg.NetBoxToDiodeAPIKey,
"DATASOURCE_TO_DIODE": cfg.DatasourceToDiodeAPIKey,
}

if err := rc.HSet(ctx, "diode.api_keys", apiKeys).Err(); err != nil {
return nil, err
}

return apiKeys, nil
}
22 changes: 21 additions & 1 deletion diode-server/reconciler/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"

"github.com/kelseyhightower/envconfig"
"github.com/redis/go-redis/v9"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
Expand All @@ -17,24 +18,43 @@ type Component struct {
logger *slog.Logger
grpcListener net.Listener
grpcServer *grpc.Server
redisClient *redis.Client
apiKeys map[string]string
}

// New creates a new reconciler component
func New(logger *slog.Logger) (*Component, error) {
func New(ctx context.Context, logger *slog.Logger) (*Component, error) {
var cfg Config
envconfig.MustProcess("", &cfg)

redisClient := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", cfg.RedisHost, cfg.RedisPort),
Password: cfg.RedisPassword,
DB: cfg.RedisDB,
})

if _, err := redisClient.Ping(ctx).Result(); err != nil {
return nil, fmt.Errorf("failed connection to %s: %v", redisClient.String(), err)
}

grpcListener, err := net.Listen("tcp", fmt.Sprintf(":%d", cfg.GRPCPort))
if err != nil {
return nil, fmt.Errorf("failed to listen on port %d: %v", cfg.GRPCPort, err)
}

apiKeys, err := loadAPIKeys(ctx, cfg, redisClient)
if err != nil {
return nil, fmt.Errorf("failed to configure data sources: %v", err)
}

grpcServer := grpc.NewServer()
component := &Component{
config: cfg,
logger: logger,
grpcListener: grpcListener,
grpcServer: grpcServer,
redisClient: redisClient,
apiKeys: apiKeys,
}
reflection.Register(grpcServer)

Expand Down
10 changes: 9 additions & 1 deletion diode-server/reconciler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,13 @@ package reconciler

// Config is the configuration for the reconciler service
type Config struct {
GRPCPort int `envconfig:"GRPC_PORT" default:"8081"`
GRPCPort int `envconfig:"GRPC_PORT" default:"8081"`
RedisHost string `envconfig:"REDIS_HOST" default:"127.0.0.1"`
RedisPort string `envconfig:"REDIS_PORT" default:"6379"`
RedisPassword string `envconfig:"REDIS_PASSWORD" required:"true"`
RedisDB int `envconfig:"REDIS_DB" default:"0"`
NetBoxAPIURL string `envconfig:"NETBOX_API_URL" required:"true"`
DiodeToNetBoxAPIKey string `envconfig:"DIODE_TO_NETBOX_API_KEY" required:"true"`
NetBoxToDiodeAPIKey string `envconfig:"NETBOX_DIODE_API_KEY" required:"true"`
DatasourceToDiodeAPIKey string `envconfig:"DATASOURCE_TO_DIODE_API_KEY" required:"true"`
}
Loading