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

Deprecate using environment variables for auth settings in sessions #121

Merged
merged 4 commits into from
Jan 31, 2024

Conversation

iwysiu
Copy link
Contributor

@iwysiu iwysiu commented Jan 30, 2024

Previously, GetSessions would use the environment variables to get the grafana aws settings. This updates it to use settings passed in from the datasource and fallback to using the environment variables if they aren't set.

I also updated the changelog so that doesn't have to be a separate commit

for grafana/grafana#81208

@@ -106,8 +105,12 @@ func (sc *SessionCache) GetSession(c SessionConfig) (*session.Session, error) {
// DefaultRegion is deprecated, Region should be used instead
c.Settings.Region = c.Settings.DefaultRegion
}
if c.AuthSettings == nil {
Copy link
Contributor Author

@iwysiu iwysiu Jan 30, 2024

Choose a reason for hiding this comment

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

If the datasource calling GetSession is getting the settings from the contexts, they'll pass the values through AuthSettings. Otherwise, we'll need to get them from the env variables.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe this comment should be in the code?

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

@iwysiu iwysiu marked this pull request as ready for review January 30, 2024 20:02
@iwysiu iwysiu requested a review from a team as a code owner January 30, 2024 20:02
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! Only the tiniest of nits in comments.

expectedDuration, err := time.ParseDuration("20m")
require.NoError(t, err)
var ctxDuration time.Duration = 600000000000 // 10 minutes in nanoseconds count
var envDuration time.Duration = 1200000000000 // 20 minutes in nanoseconds count
Copy link
Contributor

Choose a reason for hiding this comment

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

tiny nit: these could be expressed as 10 * time.Minute and 20 * time.Minute to be a bit more readable

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

expCreds := credentials.NewCredentials(&stscreds.AssumeRoleProvider{
RoleARN: roleARN,
Duration: 1200000000000, //20 minutes in nanoseconds count
var expectedDuration time.Duration = 1200000000000 //20 minutes in nanoseconds count
Copy link
Contributor

Choose a reason for hiding this comment

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

same tiny nit here

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

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.

Is there a general plan to someday remove support for this entirely? Maybe we need to make a ticket and put it in out backlog to remove this from the env and then link that throughout the code so we know when we can remove this?

@@ -91,6 +91,15 @@ func ReadAuthSettingsFromContext(ctx context.Context) (*AuthSettings, bool) {
hasSettings = true
}

if v := cfg.Get(SessionDurationEnvVarKeyName); v != "" {
Copy link
Member

Choose a reason for hiding this comment

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

Should the const still be called SessionsDurationEnvVarKeyName?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe not, but it's better practice for library maintenance to avoid renaming exported consts/functions. Technically this library is still unreleased so we could change it if we want, but the name was close enough that I thought it was fine to leave. I can rename it if we think it'd be better

@@ -106,8 +105,12 @@ func (sc *SessionCache) GetSession(c SessionConfig) (*session.Session, error) {
// DefaultRegion is deprecated, Region should be used instead
c.Settings.Region = c.Settings.DefaultRegion
}
if c.AuthSettings == nil {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this comment should be in the code?

authTypeAllowed := false
for _, provider := range sc.authSettings.AllowedAuthProviders {
Copy link
Member

Choose a reason for hiding this comment

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

this change of going over the config vs whats in the session cache seems like it could have unexpected changes? Is it related to this pr? Sorry if I missed it!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's related! To stop relying on the environment variables, we need to get the GrafanaConfig off the context, and we don't have access to the context at when we create the SessionCache in the datasource. Instead, the datasource gets the config off the context when we create a new instance, and if it doesn't it gets the config off the environment variables (what currently happens)
The only unexpected thing that I can think of is if they were setting ExternalID (which is experimental and grafana only) or SessionDuration (which isn't a documented and is basically an old experimental feature) through env variables, in which case those will be ignored. Everything else gets overwritten with defaults when Grafana starts up if they weren't set in the ini file.

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.

Is there a general plan to someday remove support for this entirely? Maybe we need to make a ticket and put it in out backlog to remove this from the env and then link that throughout the code so we know when we can remove this?

For multitenancy, we want plugins to stop relying on env variables. Yeah, made an issue #122

expectedDuration, err := time.ParseDuration("20m")
require.NoError(t, err)
var ctxDuration time.Duration = 600000000000 // 10 minutes in nanoseconds count
var envDuration time.Duration = 1200000000000 // 20 minutes in nanoseconds count
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

@@ -91,6 +91,15 @@ func ReadAuthSettingsFromContext(ctx context.Context) (*AuthSettings, bool) {
hasSettings = true
}

if v := cfg.Get(SessionDurationEnvVarKeyName); v != "" {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe not, but it's better practice for library maintenance to avoid renaming exported consts/functions. Technically this library is still unreleased so we could change it if we want, but the name was close enough that I thought it was fine to leave. I can rename it if we think it'd be better

@@ -106,8 +105,12 @@ func (sc *SessionCache) GetSession(c SessionConfig) (*session.Session, error) {
// DefaultRegion is deprecated, Region should be used instead
c.Settings.Region = c.Settings.DefaultRegion
}
if c.AuthSettings == nil {
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

expCreds := credentials.NewCredentials(&stscreds.AssumeRoleProvider{
RoleARN: roleARN,
Duration: 1200000000000, //20 minutes in nanoseconds count
var expectedDuration time.Duration = 1200000000000 //20 minutes in nanoseconds count
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

authTypeAllowed := false
for _, provider := range sc.authSettings.AllowedAuthProviders {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's related! To stop relying on the environment variables, we need to get the GrafanaConfig off the context, and we don't have access to the context at when we create the SessionCache in the datasource. Instead, the datasource gets the config off the context when we create a new instance, and if it doesn't it gets the config off the environment variables (what currently happens)
The only unexpected thing that I can think of is if they were setting ExternalID (which is experimental and grafana only) or SessionDuration (which isn't a documented and is basically an old experimental feature) through env variables, in which case those will be ignored. Everything else gets overwritten with defaults when Grafana starts up if they weren't set in the ini file.

// GrafanaListMetricsPageLimit is the string literal for the cloudwatch list metrics page limit key name
GrafanaListMetricsPageLimit = "AWS_CW_LIST_METRICS_PAGE_LIMIT"
// ListMetricsPageLimitKeyName is the string literal for the cloudwatch list metrics page limit key name
ListMetricsPageLimitKeyName = "AWS_CW_LIST_METRICS_PAGE_LIMIT"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Discussing the other variable names made me realize that I wanted to rename this one to be more in line with the others before anyone's using it

@iwysiu iwysiu merged commit 2cf8fad into main Jan 31, 2024
2 checks passed
@iwysiu iwysiu deleted the deprecate-env branch January 31, 2024 16:37
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