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

Add ReadSettings to get config settings from context #118

Merged
merged 4 commits into from
Jan 29, 2024
Merged

Conversation

iwysiu
Copy link
Contributor

@iwysiu iwysiu commented Jan 23, 2024

part of grafana/grafana#79324
The grafana part of the work is at grafana/grafana#81208

As part of the decoupling work, we need to have a way to get the grafana config off the context, so that CloudWatch plugin can use that instead of the grafana/package/setting package. This pr adds a helper to fetch the grafana config settings from the context. Previously, external plugins used a workaround where grafana would set the config values in the environment variables and the plugins would get the config values from those plugins. I'm adding this instead of using that existing env variable function because grafana wants to move away from setting configs in the env variables (for multitenancy reasons).
(for context, heavily inspired by this commit which does the same work for azure)

@iwysiu iwysiu marked this pull request as ready for review January 23, 2024 22:14
@iwysiu iwysiu requested a review from a team as a code owner January 23, 2024 22:14
Copy link
Member

@sarahzinger sarahzinger left a comment

Choose a reason for hiding this comment

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

Looks good!

defaultListMetricsPageLimit = 500
)

// ReadAuthSettingsFromEnvironmentVariables is deprecated in favor of
Copy link
Member

Choose a reason for hiding this comment

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

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, fixed

func ReadSettings(ctx context.Context) *AuthSettings {
settings, exists := ReadSettingsFromContext(ctx)
if !exists {
settings = ReadAuthSettingsFromEnvironmentVariables()
Copy link
Member

Choose a reason for hiding this comment

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

probably silly question but are there circumstances where there are settings that we'd want to read from context and from env variables? probably not???

Also Settings feels so vague to me, are there specific settings we're talking about here? I guess it's auth configuration settings from custom.ini? maybe note that in the comment above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, the context and env should be the same settings (except for session duration, which I noted below). Changed to ReadAuthSettings if that's clearer? Though technically its not just auth settings, its just whatever the grafana settings are (limitMetricsPages is not auth related).

Copy link
Contributor Author

@iwysiu iwysiu left a comment

Choose a reason for hiding this comment

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

I realized that I never pushed my initial explanatory comments while looking at yours, oops

if cfg == nil {
return settings, false
}
hasSettings := false
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The azure implementation set the precedent that if the GrafanaConfig exists but doesn't set anything, it counts as not having settings, but I'm not sure if that's actually set in stone

}

// Users set session duration directly as an environment variable
sessionDurationString := os.Getenv(SessionDurationEnvVarKeyName)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can make a follow up issue to add this to the config file, but I figured that this was a big enough and time sensitive enough change without it. (this option was added in #58 but not to the config)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added an issue for it here: grafana/grafana#81414

@@ -15,11 +15,16 @@ import (
// AmazonSessionProvider will return a session (perhaps cached) for given region and settings
type AmazonSessionProvider func(region string, s AWSDatasourceSettings) (*session.Session, error)

// AuthSettings defines whether certain auth types and provider can be used or not
// AuthSettings stores the AWS settings from Grafana
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Technically these aren't really just auth settings anymore, but for simplicity and non-breaking changes I put the additional settings here.

func ReadSettings(ctx context.Context) *AuthSettings {
settings, exists := ReadSettingsFromContext(ctx)
if !exists {
settings = ReadAuthSettingsFromEnvironmentVariables()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, the context and env should be the same settings (except for session duration, which I noted below). Changed to ReadAuthSettings if that's clearer? Though technically its not just auth settings, its just whatever the grafana settings are (limitMetricsPages is not auth related).

defaultListMetricsPageLimit = 500
)

// ReadAuthSettingsFromEnvironmentVariables is deprecated in favor of
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, fixed

Copy link
Contributor

@njvrzm njvrzm left a comment

Choose a reason for hiding this comment

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

Looks good to me! Comments are all optional.

pkg/awsds/authSettings.go Outdated Show resolved Hide resolved
pkg/awsds/authSettings.go Outdated Show resolved Hide resolved
} else {
settings.SessionDuration = &sessionDuration
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Totally optional, but while you're in here, you could pull in the default value for SessionDuration out of sessions.go:

duration := stscreds.DefaultDuration
if sc.authSettings.SessionDuration != nil {
duration = *sc.authSettings.SessionDuration
}

so that all the default values are set in the same place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in the default settings function

)

// ReadAuthSettingsFromEnvironmentVariables gets the Grafana auth settings from the environment variables
// Deprecated: Use ReadAuthSettingsFromContext instead
func ReadAuthSettingsFromEnvironmentVariables() *AuthSettings {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a little funny that ReadAuthSettingsFromEnvironmentVariables is in sessions.go but ReadAuthSettingsFromConfig is in authSettings.go now. Certainly don't need to move it, especially since it's deprecated, but it's nice to tidy up :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done! moved this and the consts

@iwysiu iwysiu merged commit 3de251f into main Jan 29, 2024
3 checks passed
@iwysiu iwysiu deleted the decouple-settings branch January 29, 2024 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants