Skip to content

Commit

Permalink
restructure config
Browse files Browse the repository at this point in the history
Signed-off-by: WashingtonKK <[email protected]>
  • Loading branch information
WashingtonKK committed Nov 7, 2024
1 parent c74179e commit 26c024f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 38 deletions.
12 changes: 6 additions & 6 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ func main() {

grpcServerConfig := server.AgentConfig{
BaseConfig: grpcClient.BaseConfig{
URL: fmt.Sprintf("%s:%s", cfg.AgentConfig.Host, cfg.AgentConfig.Port),
URL: fmt.Sprintf("%s:%s", cfg.AgentConfig.Host, cfg.AgentConfig.Port),
CertFile: cfg.AgentConfig.CertFile,
KeyFile: cfg.AgentConfig.KeyFile,
ServerCAFile: cfg.AgentConfig.ServerCAFile,
ClientCAFile: cfg.AgentConfig.ClientCAFile,
},
CertFile: cfg.AgentConfig.CertFile,
KeyFile: cfg.AgentConfig.KeyFile,
ServerCAFile: cfg.AgentConfig.ServerCAFile,
ClientCAFile: cfg.AgentConfig.ClientCAFile,
AttestedTLS: cfg.AgentConfig.AttestedTls,
AttestedTLS: cfg.AgentConfig.AttestedTls,
}

registerAgentServiceServer := func(srv *grpc.Server) {
Expand Down
6 changes: 3 additions & 3 deletions internal/server/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ func TestServerStartWithTLS(t *testing.T) {

config := server.AgentConfig{
BaseConfig: grpcClient.BaseConfig{
URL: "localhost:0",
URL: "localhost:0",
CertFile: string(cert),
KeyFile: string(key),
},
CertFile: string(cert),
KeyFile: string(key),
}

logBuffer := &ThreadSafeBuffer{}
Expand Down
6 changes: 1 addition & 5 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ type Server interface {

type AgentConfig struct {
grpc.BaseConfig
CertFile string `env:"SERVER_CERT" envDefault:""`
KeyFile string `env:"SERVER_KEY" envDefault:""`
ServerCAFile string `env:"SERVER_CA_CERTS" envDefault:""`
ClientCAFile string `env:"CLIENT_CA_CERTS" envDefault:""`
AttestedTLS bool `env:"ATTESTED_TLS" envDefault:"false"`
AttestedTLS bool `env:"ATTESTED_TLS" envDefault:"false"`
}

type BaseServer struct {
Expand Down
18 changes: 10 additions & 8 deletions pkg/clients/grpc/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,20 @@ var (
)

type BaseConfig struct {
URL string `env:"URL" envDefault:"localhost:7001"`
Timeout time.Duration `env:"TIMEOUT" envDefault:"60s"`
URL string `env:"URL" envDefault:"localhost:7001"`
Timeout time.Duration `env:"TIMEOUT" envDefault:"60s"`
ClientCert string `env:"CLIENT_CERT" envDefault:""`
ClientKey string `env:"CLIENT_KEY" envDefault:""`
ServerCAFile string `env:"SERVER_CA_CERTS" envDefault:""`
CertFile string `env:"SERVER_CERT" envDefault:""`
KeyFile string `env:"SERVER_KEY" envDefault:""`
ClientCAFile string `env:"CLIENT_CA_CERTS" envDefault:""`
}

type ManagerConfig struct {
BaseConfig
ClientCert string `env:"CLIENT_CERT" envDefault:""`
ClientKey string `env:"CLIENT_KEY" envDefault:""`
ServerCAFile string `env:"SERVER_CA_CERTS" envDefault:""`
BackendInfo string `env:"BACKEND_INFO" envDefault:""`
ClientTLS bool `env:"CLIENT_TLS" envDefault:"false"`

BackendInfo string `env:"BACKEND_INFO" envDefault:""`
ClientTLS bool `env:"CLIENT_TLS" envDefault:"false"`
}

type Client interface {
Expand Down
32 changes: 16 additions & 16 deletions pkg/clients/grpc/connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func TestNewClient(t *testing.T) {
name: "Success with TLS",
cfg: ManagerConfig{
BaseConfig: BaseConfig{
URL: "localhost:7001",
URL: "localhost:7001",
ServerCAFile: caCertFile,
},
ServerCAFile: caCertFile,
},
wantErr: false,
err: nil,
Expand All @@ -61,11 +61,11 @@ func TestNewClient(t *testing.T) {
name: "Success with mTLS",
cfg: ManagerConfig{
BaseConfig: BaseConfig{
URL: "localhost:7001",
URL: "localhost:7001",
ServerCAFile: caCertFile,
ClientCert: clientCertFile,
ClientKey: clientKeyFile,
},
ServerCAFile: caCertFile,
ClientCert: clientCertFile,
ClientKey: clientKeyFile,
},
wantErr: false,
err: nil,
Expand All @@ -74,9 +74,9 @@ func TestNewClient(t *testing.T) {
name: "Fail with invalid ServerCAFile",
cfg: ManagerConfig{
BaseConfig: BaseConfig{
URL: "localhost:7001",
URL: "localhost:7001",
ServerCAFile: "nonexistent.pem",
},
ServerCAFile: "nonexistent.pem",
},
wantErr: true,
err: errFailedToLoadRootCA,
Expand All @@ -85,11 +85,11 @@ func TestNewClient(t *testing.T) {
name: "Fail with invalid ClientCert",
cfg: ManagerConfig{
BaseConfig: BaseConfig{
URL: "localhost:7001",
URL: "localhost:7001",
ServerCAFile: caCertFile,
ClientCert: "nonexistent.pem",
ClientKey: clientKeyFile,
},
ServerCAFile: caCertFile,
ClientCert: "nonexistent.pem",
ClientKey: clientKeyFile,
},
wantErr: true,
err: errFailedToLoadClientCertKey,
Expand All @@ -98,11 +98,11 @@ func TestNewClient(t *testing.T) {
name: "Fail with invalid ClientKey",
cfg: ManagerConfig{
BaseConfig: BaseConfig{
URL: "localhost:7001",
URL: "localhost:7001",
ServerCAFile: caCertFile,
ClientCert: clientCertFile,
ClientKey: "nonexistent.pem",
},
ServerCAFile: caCertFile,
ClientCert: clientCertFile,
ClientKey: "nonexistent.pem",
},
wantErr: true,
err: errFailedToLoadClientCertKey,
Expand Down

0 comments on commit 26c024f

Please sign in to comment.