feat: pass gcom sso_api_token to repo created from install command (#98973)
* feat: pass gcom sso_api_token to repo created from install command * fix * fix: extract gcom section to a func * Update pkg/cmd/grafana-cli/utils/command_line.go Co-authored-by: Giuseppe Guerra <giuseppe.guerra@grafana.com> * fix: only set gcom token when the request is to GCOM --------- Co-authored-by: Giuseppe Guerra <giuseppe.guerra@grafana.com>
This commit is contained in:
co-authored by
Giuseppe Guerra
parent
79fc26ea87
commit
dfe0712955
@@ -90,6 +90,7 @@ type pluginInstallOpts struct {
|
||||
repoURL string
|
||||
pluginURL string
|
||||
pluginDir string
|
||||
gcomToken string
|
||||
}
|
||||
|
||||
func newInstallPluginOpts(c utils.CommandLine) pluginInstallOpts {
|
||||
@@ -98,6 +99,7 @@ func newInstallPluginOpts(c utils.CommandLine) pluginInstallOpts {
|
||||
repoURL: c.PluginRepoURL(),
|
||||
pluginURL: c.PluginURL(),
|
||||
pluginDir: c.PluginDirectory(),
|
||||
gcomToken: c.GcomToken(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,9 +134,10 @@ func doInstallPlugin(ctx context.Context, pluginID, version string, o pluginInst
|
||||
}
|
||||
|
||||
repository := repo.NewManager(repo.ManagerCfg{
|
||||
SkipTLSVerify: o.insecure,
|
||||
BaseURL: o.repoURL,
|
||||
Logger: services.Logger,
|
||||
SkipTLSVerify: o.insecure,
|
||||
BaseURL: o.repoURL,
|
||||
Logger: services.Logger,
|
||||
GrafanaComAPIToken: o.gcomToken,
|
||||
})
|
||||
|
||||
// FIXME: Re-enable grafanaVersion. This check was broken in 10.2 so disabling it for the moment.
|
||||
|
||||
@@ -106,7 +106,8 @@ func TestValidatePluginRepoConfig(t *testing.T) {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
grafDir, cfgPath := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
|
||||
GrafanaComAPIURL: "https://grafana-dev.com",
|
||||
GrafanaComAPIURL: "https://grafana-dev.com",
|
||||
GrafanaComSSOAPIToken: "token3",
|
||||
})
|
||||
|
||||
c, err := commandstest.NewCliContext(map[string]string{
|
||||
@@ -116,6 +117,9 @@ func TestValidatePluginRepoConfig(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
repoURL := c.PluginRepoURL()
|
||||
require.Equal(t, "https://grafana-dev.com/plugins", repoURL)
|
||||
|
||||
token := c.GcomToken()
|
||||
require.Equal(t, "token3", token)
|
||||
})
|
||||
|
||||
t.Run("Should use config overrides parameter if it is set alongside config parameter", func(t *testing.T) {
|
||||
|
||||
@@ -26,6 +26,7 @@ type CommandLine interface {
|
||||
PluginDirectory() string
|
||||
PluginRepoURL() string
|
||||
PluginURL() string
|
||||
GcomToken() string
|
||||
}
|
||||
|
||||
type ApiClient interface {
|
||||
@@ -75,12 +76,7 @@ func (c *ContextCommandLine) PluginRepoURL() string {
|
||||
|
||||
// if --config flag is set, try to get the GrafanaComAPIURL setting
|
||||
if c.ConfigFile() != "" {
|
||||
configOptions := strings.Split(c.String("configOverrides"), " ")
|
||||
cfg, err := setting.NewCfgFromArgs(setting.CommandLineArgs{
|
||||
Config: c.ConfigFile(),
|
||||
HomePath: c.HomePath(),
|
||||
Args: append(configOptions, c.Args().Slice()...),
|
||||
})
|
||||
cfg, err := c.Config()
|
||||
|
||||
if err != nil {
|
||||
logger.Debug("Could not parse config file", err)
|
||||
@@ -92,6 +88,25 @@ func (c *ContextCommandLine) PluginRepoURL() string {
|
||||
return c.String("repo")
|
||||
}
|
||||
|
||||
func (c *ContextCommandLine) Config() (*setting.Cfg, error) {
|
||||
configOptions := strings.Split(c.String("configOverrides"), " ")
|
||||
return setting.NewCfgFromArgs(setting.CommandLineArgs{
|
||||
Config: c.ConfigFile(),
|
||||
HomePath: c.HomePath(),
|
||||
Args: append(configOptions, c.Args().Slice()...),
|
||||
})
|
||||
}
|
||||
|
||||
func (c *ContextCommandLine) GcomToken() string {
|
||||
cfg, err := c.Config()
|
||||
|
||||
if err != nil {
|
||||
logger.Debug("Could not parse config file", err)
|
||||
return ""
|
||||
}
|
||||
return cfg.GrafanaComSSOAPIToken
|
||||
}
|
||||
|
||||
func (c *ContextCommandLine) PluginURL() string {
|
||||
return c.String("pluginUrl")
|
||||
}
|
||||
|
||||
@@ -146,6 +146,19 @@ func (_m *MockCommandLine) PluginURL() string {
|
||||
return r0
|
||||
}
|
||||
|
||||
func (_m *MockCommandLine) GcomToken() string {
|
||||
ret := _m.Called()
|
||||
|
||||
var r0 string
|
||||
if rf, ok := ret.Get(0).(func() string); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
r0 = ret.Get(0).(string)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// ShowHelp provides a mock function with given fields:
|
||||
func (_m *MockCommandLine) ShowHelp() error {
|
||||
ret := _m.Called()
|
||||
|
||||
Reference in New Issue
Block a user