-
Notifications
You must be signed in to change notification settings - Fork 20
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
RHCLOUD-35503 updates defaults and adds config pkg #176
Merged
tonytheleg
merged 1 commit into
project-kessel:main
from
tonytheleg:RHCLOUD-35503-add-config-pkg
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"github.com/go-kratos/kratos/v2/log" | ||
"github.com/project-kessel/inventory-api/internal/authn" | ||
"github.com/project-kessel/inventory-api/internal/authz" | ||
"github.com/project-kessel/inventory-api/internal/eventing" | ||
"github.com/project-kessel/inventory-api/internal/server" | ||
"github.com/project-kessel/inventory-api/internal/storage" | ||
clowder "github.com/redhatinsights/app-common-go/pkg/api/v1" | ||
"strconv" | ||
) | ||
|
||
// OptionsConfig contains the settings for each configuration option | ||
type OptionsConfig struct { | ||
Authn *authn.Options | ||
Authz *authz.Options | ||
Storage *storage.Options | ||
Eventing *eventing.Options | ||
Server *server.Options | ||
} | ||
|
||
// NewOptionsConfig returns a new OptionsConfig with default options set | ||
func NewOptionsConfig() *OptionsConfig { | ||
return &OptionsConfig{ | ||
authn.NewOptions(), | ||
authz.NewOptions(), | ||
storage.NewOptions(), | ||
eventing.NewOptions(), | ||
server.NewOptions(), | ||
} | ||
} | ||
|
||
// LogConfigurationInfo outputs connection details to logs when in debug for testing (no secret data is output) | ||
func LogConfigurationInfo(options *OptionsConfig) { | ||
log.Debugf("Server Configuration: Public URL: %s, HTTP Listener: %s, GRPC Listener: %s", | ||
options.Server.PublicUrl, | ||
options.Server.HttpOptions.Addr, | ||
options.Server.GrpcOptions.Addr) | ||
|
||
if options.Authn.Oidc.AuthorizationServerURL != "" { | ||
log.Debugf("Authn Configuration: URL: %s, ClientID: %s", | ||
options.Authn.Oidc.AuthorizationServerURL, | ||
options.Authn.Oidc.ClientId, | ||
) | ||
} | ||
|
||
if options.Storage.Database == storage.Postgres { | ||
log.Debugf("Storage Configuration: Host: %s, DB: %s, Port: %s", | ||
options.Storage.Postgres.Host, | ||
options.Storage.Postgres.DbName, | ||
options.Storage.Postgres.Port, | ||
) | ||
} | ||
|
||
if options.Authz.Authz == authz.Kessel { | ||
log.Debugf("Authz Configuration: URL: %s, Insecure?: %t, OIDC?: %t", | ||
options.Authz.Kessel.URL, | ||
options.Authz.Kessel.Insecure, | ||
options.Authz.Kessel.EnableOidcAuth, | ||
) | ||
} | ||
} | ||
|
||
// InjectClowdAppConfig updates service options based on values in the ClowdApp AppConfig | ||
func (o *OptionsConfig) InjectClowdAppConfig() { | ||
// check for authz config | ||
for _, endpoint := range clowder.LoadedConfig.Endpoints { | ||
if endpoint.App == authz.RelationsAPI { | ||
o.ConfigureAuthz(endpoint) | ||
} | ||
} | ||
// check for db config | ||
if clowder.LoadedConfig.Database != nil { | ||
o.ConfigureStorage(clowder.LoadedConfig.Database) | ||
} | ||
} | ||
|
||
// ConfigureAuthz updates Authz settings based on ClowdApp AppConfig | ||
func (o *OptionsConfig) ConfigureAuthz(endpoint clowder.DependencyEndpoint) { | ||
o.Authz.Authz = authz.Kessel | ||
o.Authz.Kessel.URL = fmt.Sprintf("%s:%d", endpoint.Hostname, 9000) | ||
} | ||
|
||
// ConfigureStorage updates Storage settings based on ClowdApp AppConfig | ||
func (o *OptionsConfig) ConfigureStorage(database *clowder.DatabaseConfig) { | ||
o.Storage.Database = storage.Postgres | ||
o.Storage.Postgres.Host = database.Hostname | ||
o.Storage.Postgres.Port = strconv.Itoa(database.Port) | ||
o.Storage.Postgres.User = database.Username | ||
o.Storage.Postgres.Password = database.Password | ||
o.Storage.Postgres.DbName = database.Name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package config | ||
|
||
import ( | ||
clowder "github.com/redhatinsights/app-common-go/pkg/api/v1" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestConfigureStorage(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
appconfig *clowder.AppConfig | ||
options *OptionsConfig | ||
}{ | ||
{ | ||
name: "ensures DB info is set", | ||
appconfig: &clowder.AppConfig{ | ||
Database: &clowder.DatabaseConfig{ | ||
Hostname: "postgres", | ||
Name: "postgres", | ||
Port: 5432, | ||
Username: "db-user", | ||
Password: "db-password", | ||
}, | ||
}, | ||
options: NewOptionsConfig(), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
test.options.ConfigureStorage(test.appconfig.Database) | ||
assert.Equal(t, "postgres", test.options.Storage.Database) | ||
assert.Equal(t, "postgres", test.options.Storage.Postgres.Host) | ||
assert.Equal(t, "postgres", test.options.Storage.Postgres.DbName) | ||
assert.Equal(t, "5432", test.options.Storage.Postgres.Port) | ||
assert.Equal(t, "db-user", test.options.Storage.Postgres.User) | ||
assert.Equal(t, "db-password", test.options.Storage.Postgres.Password) | ||
}) | ||
} | ||
} | ||
|
||
func TestConfigureAuthz(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
appconfig *clowder.AppConfig | ||
options *OptionsConfig | ||
}{ | ||
{ | ||
name: "ensures Authz info is set", | ||
appconfig: &clowder.AppConfig{ | ||
Endpoints: []clowder.DependencyEndpoint{ | ||
clowder.DependencyEndpoint{ | ||
Hostname: "kessel-relations", | ||
}, | ||
}, | ||
}, | ||
options: NewOptionsConfig(), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
test.options.ConfigureAuthz(test.appconfig.Endpoints[0]) | ||
assert.Equal(t, "kessel", test.options.Authz.Authz) | ||
assert.Equal(t, "kessel-relations:9000", test.options.Authz.Kessel.URL) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here
Kessel
Refers torelations-api
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason for adding the
RelationsAPI = "kessel-relations"
const is just so that i can reference that specific value HERE instead of hardcording "kessel-relations" in that method as this is the expected name of the relations ClowdApp that shows up in appconfig. This way if it ever changes, we just change the constSimilar to how the authz.impl config has a Kessel const which i use to set the impl value to 'kessel' in
inventory-api/internal/config/config.go
Line 82 in f8d5a3c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest changing the
Kessel
toRelationsAPI
. authz.Kessel and RelationsAPI both are referring to "kessel-relations"