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

ETOS SSE v2alpha #91

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 2 additions & 3 deletions cmd/keys/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ func main() {
"name": "ETOS API",
})

log.Info("Loading Key routes")

pub, err := cfg.PublicKey()
if err != nil {
log.Fatal(err.Error())
Expand All @@ -76,8 +74,9 @@ func main() {
}
v1AlphaKeys := v1alpha.New(ctx, cfg, log, authorizer)
defer v1AlphaKeys.Close()
app := application.New(v1AlphaKeys)

log.Info("Loading Key routes")
app := application.New(v1AlphaKeys)
srv := server.NewWebService(cfg, log, app)

done := make(chan os.Signal, 1)
Expand Down
29 changes: 9 additions & 20 deletions internal/config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,19 @@ type Config interface {
ETOSNamespace() string
DatabaseURI() string
PublicKey() ([]byte, error)
PrivateKey() ([]byte, error)
}

// baseCfg implements the Config interface.
type baseCfg struct {
serviceHost string
servicePort string
stripPrefix string
logLevel string
logFilePath string
etosNamespace string
databaseHost string
databasePort string
publicKeyPath string
privateKeyPath string
serviceHost string
servicePort string
stripPrefix string
logLevel string
logFilePath string
etosNamespace string
databaseHost string
databasePort string
publicKeyPath string
}

// load the command line vars for a base configuration.
Expand All @@ -61,7 +59,6 @@ func load() Config {
flag.StringVar(&conf.databaseHost, "databasehost", EnvOrDefault("ETOS_ETCD_HOST", "etcd-client"), "Host to the database.")
flag.StringVar(&conf.databasePort, "databaseport", EnvOrDefault("ETOS_ETCD_PORT", "2379"), "Port to the database.")
flag.StringVar(&conf.publicKeyPath, "publickeypath", os.Getenv("PUBLIC_KEY_PATH"), "Path to a public key to use for verifying JWTs.")
flag.StringVar(&conf.privateKeyPath, "privatekeypath", os.Getenv("PRIVATE_KEY_PATH"), "Path to a private key to use for signing JWTs.")
return &conf
}

Expand Down Expand Up @@ -108,14 +105,6 @@ func (c *baseCfg) PublicKey() ([]byte, error) {
return os.ReadFile(c.publicKeyPath)
}

// PrivateKey reads a private key from disk and returns the content.
func (c *baseCfg) PrivateKey() ([]byte, error) {
if c.privateKeyPath == "" {
return nil, nil
}
return os.ReadFile(c.privateKeyPath)
}

// EnvOrDefault will look up key in environment variables and return if it exists, else return the fallback value.
func EnvOrDefault(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
Expand Down
29 changes: 26 additions & 3 deletions internal/config/keys.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't the public key handled in this file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's handled in base since it can be used by serveral services for verifying signatures. The privatekey is only for signing which the keys service is responsible for

Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,38 @@
// limitations under the License.
package config

import "flag"
import (
"flag"
"os"
)

type KeyConfig interface {
Config
PrivateKey() ([]byte, error)
}

// keyCfg implements the KeyConfig interface.
type keyCfg struct {
Config
privateKeyPath string
}

// NewKeyConcifg creates a key config interface based on input parameters or environment variables.
func NewKeyConfig() KeyConfig {
cfg := load()
var conf keyCfg

flag.StringVar(&conf.privateKeyPath, "privatekeypath", os.Getenv("PRIVATE_KEY_PATH"), "Path to a private key to use for signing JWTs.")
base := load()
flag.Parse()
return cfg
conf.Config = base

return &conf
}

// PrivateKey reads a private key from disk and returns the content.
func (c *keyCfg) PrivateKey() ([]byte, error) {
if c.privateKeyPath == "" {
return nil, nil
}
return os.ReadFile(c.privateKeyPath)
}
10 changes: 10 additions & 0 deletions test/testconfig/testconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func (c *cfg) ServicePort() string {
return c.servicePort
}

// StripPrefix returns the prefix to strip. Empty string if no prefix.
func (c *cfg) StripPrefix() string {
return ""
}

// LogLevel returns the Log level testconfig parameter
func (c *cfg) LogLevel() string {
return c.logLevel
Expand All @@ -74,3 +79,8 @@ func (c *cfg) ETOSNamespace() string {
func (c *cfg) DatabaseURI() string {
return "etcd-client:2379"
}

// PublicKey returns a public key.
func (c *cfg) PublicKey() ([]byte, error) {
return nil, nil
}