| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | package setting | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-06-20 22:15:21 +08:00
										 |  |  | 	"bufio" | 
					
						
							| 
									
										
										
										
											2021-02-01 17:13:09 +08:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2015-02-12 20:31:41 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2019-04-25 14:29:07 +08:00
										 |  |  | 	"path" | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2017-06-19 21:36:08 +08:00
										 |  |  | 	"runtime" | 
					
						
							| 
									
										
										
										
											2019-06-20 22:15:21 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2024-02-05 23:25:54 +08:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2020-09-14 21:57:38 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 22:16:40 +08:00
										 |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							| 
									
										
										
										
											2020-03-11 17:04:48 +08:00
										 |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							| 
									
										
										
										
											2019-06-12 13:27:47 +08:00
										 |  |  | 	"gopkg.in/ini.v1" | 
					
						
							| 
									
										
										
										
											2024-03-01 18:31:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/util/osutil" | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-12 13:27:47 +08:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	windows = "windows" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | func TestLoadingSettings(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	skipStaticRootValidation = true | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	t.Run("Given the default ini files", func(t *testing.T) { | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		err := cfg.Load(CommandLineArgs{HomePath: "../../", Config: "../../conf/defaults.ini"}) | 
					
						
							|  |  |  | 		require.Nil(t, err) | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		require.Equal(t, "admin", cfg.AdminUser) | 
					
						
							|  |  |  | 		require.Equal(t, "http://localhost:3000/", cfg.RendererCallbackUrl) | 
					
						
							| 
									
										
										
										
											2023-05-08 23:11:36 +08:00
										 |  |  | 		require.Equal(t, "TLS1.2", cfg.MinTLSVersion) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-02-12 20:31:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	t.Run("default.ini should have no semi-colon commented entries", func(t *testing.T) { | 
					
						
							|  |  |  | 		file, err := os.Open("../../conf/defaults.ini") | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Errorf("failed to load defaults.ini file: %v", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		defer func() { | 
					
						
							|  |  |  | 			err := file.Close() | 
					
						
							|  |  |  | 			require.Nil(t, err) | 
					
						
							|  |  |  | 		}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		scanner := bufio.NewScanner(file) | 
					
						
							|  |  |  | 		for scanner.Scan() { | 
					
						
							|  |  |  | 			// This only catches values commented out with ";" and will not catch those that are commented out with "#".
 | 
					
						
							|  |  |  | 			if strings.HasPrefix(scanner.Text(), ";") { | 
					
						
							|  |  |  | 				t.Errorf("entries in defaults.ini must not be commented or environment variables will not work: %v", scanner.Text()) | 
					
						
							| 
									
										
										
										
											2019-06-20 22:15:21 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-07-22 22:50:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	t.Run("sample.ini should load successfully", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 		oldCustomInitPath := customInitPath | 
					
						
							|  |  |  | 		customInitPath = "conf/sample.ini" | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		err := cfg.Load(CommandLineArgs{HomePath: "../../"}) | 
					
						
							|  |  |  | 		require.Nil(t, err) | 
					
						
							|  |  |  | 		// Restore CustomInitPath to avoid side effects.
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 		customInitPath = oldCustomInitPath | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	t.Run("Should be able to override via environment variables", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2023-06-05 17:31:03 +08:00
										 |  |  | 		t.Setenv("GF_SECURITY_ADMIN_USER", "superduper") | 
					
						
							| 
									
										
										
										
											2015-02-12 20:31:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2023-06-05 17:31:03 +08:00
										 |  |  | 		err := cfg.Load(CommandLineArgs{HomePath: "../../"}) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		require.Nil(t, err) | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		require.Equal(t, "superduper", cfg.AdminUser) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 		require.Equal(t, filepath.Join(cfg.HomePath, "data"), cfg.DataPath) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		require.Equal(t, filepath.Join(cfg.DataPath, "log"), cfg.LogsPath) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-08 21:36:15 +08:00
										 |  |  | 	t.Run("Should be able to expand parameter from environment variables", func(t *testing.T) { | 
					
						
							|  |  |  | 		t.Setenv("DEFAULT_IDP_URL", "grafana.com") | 
					
						
							|  |  |  | 		t.Setenv("GF_AUTH_GENERIC_OAUTH_AUTH_URL", "${DEFAULT_IDP_URL}/auth") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		err := cfg.Load(CommandLineArgs{HomePath: "../../"}) | 
					
						
							|  |  |  | 		require.Nil(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		genericOAuthSection, err := cfg.Raw.GetSection("auth.generic_oauth") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		require.Equal(t, "grafana.com/auth", genericOAuthSection.Key("auth_url").Value()) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	t.Run("Should replace password when defined in environment", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2023-06-05 17:31:03 +08:00
										 |  |  | 		t.Setenv("GF_SECURITY_ADMIN_PASSWORD", "supersecret") | 
					
						
							| 
									
										
										
										
											2016-06-29 00:37:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2023-06-05 17:31:03 +08:00
										 |  |  | 		err := cfg.Load(CommandLineArgs{HomePath: "../../"}) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		require.Nil(t, err) | 
					
						
							| 
									
										
										
										
											2016-06-29 00:37:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 		require.Contains(t, cfg.appliedEnvOverrides, "GF_SECURITY_ADMIN_PASSWORD=*********") | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	t.Run("Should replace password in URL when url environment is defined", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2023-06-05 17:31:03 +08:00
										 |  |  | 		t.Setenv("GF_DATABASE_URL", "mysql://user:secret@localhost:3306/database") | 
					
						
							| 
									
										
										
										
											2016-06-29 00:37:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2023-06-05 17:31:03 +08:00
										 |  |  | 		err := cfg.Load(CommandLineArgs{HomePath: "../../"}) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		require.Nil(t, err) | 
					
						
							| 
									
										
										
										
											2016-06-29 00:37:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 		require.Contains(t, cfg.appliedEnvOverrides, "GF_DATABASE_URL=mysql://user:xxxxx@localhost:3306/database") | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	t.Run("Should get property map from command line args array", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2021-11-09 00:56:56 +08:00
										 |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		props := cfg.getCommandLineProperties([]string{"cfg:test=value", "cfg:map.test=1"}) | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		require.Equal(t, 2, len(props)) | 
					
						
							|  |  |  | 		require.Equal(t, "value", props["test"]) | 
					
						
							|  |  |  | 		require.Equal(t, "1", props["map.test"]) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	t.Run("Should be able to override via command line", func(t *testing.T) { | 
					
						
							|  |  |  | 		if runtime.GOOS == windows { | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 			cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 			err := cfg.Load(CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2015-04-12 15:15:49 +08:00
										 |  |  | 				HomePath: "../../", | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 				Args:     []string{`cfg:paths.data=c:\tmp\data`, `cfg:paths.logs=c:\tmp\logs`}, | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Nil(t, err) | 
					
						
							|  |  |  | 			require.Equal(t, `c:\tmp\data`, cfg.DataPath) | 
					
						
							|  |  |  | 			require.Equal(t, `c:\tmp\logs`, cfg.LogsPath) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			cfg := NewCfg() | 
					
						
							|  |  |  | 			err := cfg.Load(CommandLineArgs{ | 
					
						
							|  |  |  | 				HomePath: "../../", | 
					
						
							|  |  |  | 				Args:     []string{"cfg:paths.data=/tmp/data", "cfg:paths.logs=/tmp/logs"}, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			require.Nil(t, err) | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Equal(t, "/tmp/data", cfg.DataPath) | 
					
						
							|  |  |  | 			require.Equal(t, "/tmp/logs", cfg.LogsPath) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	t.Run("Should be able to override defaults via command line", func(t *testing.T) { | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		err := cfg.Load(CommandLineArgs{ | 
					
						
							|  |  |  | 			HomePath: "../../", | 
					
						
							|  |  |  | 			Args: []string{ | 
					
						
							|  |  |  | 				"cfg:default.server.domain=test2", | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 			Config: filepath.Join("../../", "pkg/setting/testdata/override.ini"), | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		require.Nil(t, err) | 
					
						
							| 
									
										
										
										
											2015-04-09 18:16:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		require.Equal(t, "test2", cfg.Domain) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-02-12 20:31:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-08 23:11:36 +08:00
										 |  |  | 	t.Run("Should be able to override TLS version via command line", func(t *testing.T) { | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		err := cfg.Load(CommandLineArgs{ | 
					
						
							|  |  |  | 			HomePath: "../../", | 
					
						
							|  |  |  | 			Args: []string{ | 
					
						
							|  |  |  | 				"cfg:default.server.min_tls_version=TLS1.3", | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 			Config: filepath.Join("../../", "pkg/setting/testdata/override.ini"), | 
					
						
							| 
									
										
										
										
											2023-05-08 23:11:36 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 		require.Nil(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		require.Equal(t, "TLS1.3", cfg.MinTLSVersion) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	t.Run("Defaults can be overridden in specified config file", func(t *testing.T) { | 
					
						
							|  |  |  | 		if runtime.GOOS == windows { | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 			cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 			err := cfg.Load(CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2016-06-02 20:32:17 +08:00
										 |  |  | 				HomePath: "../../", | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 				Config:   filepath.Join("../../", "pkg/setting/testdata/override_windows.ini"), | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 				Args:     []string{`cfg:default.paths.data=c:\tmp\data`}, | 
					
						
							| 
									
										
										
										
											2016-06-02 20:32:17 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Nil(t, err) | 
					
						
							| 
									
										
										
										
											2016-06-02 20:32:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Equal(t, `c:\tmp\override`, cfg.DataPath) | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2018-09-04 19:42:55 +08:00
										 |  |  | 			cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 			err := cfg.Load(CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2018-09-04 19:42:55 +08:00
										 |  |  | 				HomePath: "../../", | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 				Config:   filepath.Join("../../", "pkg/setting/testdata/override.ini"), | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 				Args:     []string{"cfg:default.paths.data=/tmp/data"}, | 
					
						
							| 
									
										
										
										
											2018-09-04 19:42:55 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Nil(t, err) | 
					
						
							| 
									
										
										
										
											2018-09-04 19:42:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Equal(t, "/tmp/override", cfg.DataPath) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	t.Run("Command line overrides specified config file", func(t *testing.T) { | 
					
						
							|  |  |  | 		if runtime.GOOS == windows { | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 			cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 			err := cfg.Load(CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 				HomePath: "../../", | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 				Config:   filepath.Join("../../", "pkg/setting/testdata/override_windows.ini"), | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 				Args:     []string{`cfg:paths.data=c:\tmp\data`}, | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Nil(t, err) | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Equal(t, `c:\tmp\data`, cfg.DataPath) | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 			cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 			err := cfg.Load(CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 				HomePath: "../../", | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 				Config:   filepath.Join("../../", "pkg/setting/testdata/override.ini"), | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 				Args:     []string{"cfg:paths.data=/tmp/data"}, | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Nil(t, err) | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Equal(t, "/tmp/data", cfg.DataPath) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	t.Run("Can use environment variables in config values", func(t *testing.T) { | 
					
						
							|  |  |  | 		if runtime.GOOS == windows { | 
					
						
							| 
									
										
										
										
											2023-06-05 17:31:03 +08:00
										 |  |  | 			t.Setenv("GF_DATA_PATH", `c:\tmp\env_override`) | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 			cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2023-06-05 17:31:03 +08:00
										 |  |  | 			err := cfg.Load(CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 				HomePath: "../../", | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 				Args:     []string{"cfg:paths.data=${GF_DATA_PATH}"}, | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Nil(t, err) | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Equal(t, `c:\tmp\env_override`, cfg.DataPath) | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2023-06-05 17:31:03 +08:00
										 |  |  | 			t.Setenv("GF_DATA_PATH", "/tmp/env_override") | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 			cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2023-06-05 17:31:03 +08:00
										 |  |  | 			err := cfg.Load(CommandLineArgs{ | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 				HomePath: "../../", | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 				Args:     []string{"cfg:paths.data=${GF_DATA_PATH}"}, | 
					
						
							| 
									
										
										
										
											2019-11-07 18:24:54 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Nil(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			require.Equal(t, "/tmp/env_override", cfg.DataPath) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("instance_name default to hostname even if hostname env is empty", func(t *testing.T) { | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		err := cfg.Load(CommandLineArgs{ | 
					
						
							|  |  |  | 			HomePath: "../../", | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		require.Nil(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		hostname, err := os.Hostname() | 
					
						
							|  |  |  | 		require.Nil(t, err) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 		require.Equal(t, hostname, cfg.InstanceName) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("Reading callback_url should add trailing slash", func(t *testing.T) { | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		err := cfg.Load(CommandLineArgs{ | 
					
						
							|  |  |  | 			HomePath: "../../", | 
					
						
							|  |  |  | 			Args:     []string{"cfg:rendering.callback_url=http://myserver/renderer"}, | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		require.Nil(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		require.Equal(t, "http://myserver/renderer/", cfg.RendererCallbackUrl) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("Only sync_ttl should return the value sync_ttl", func(t *testing.T) { | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		err := cfg.Load(CommandLineArgs{ | 
					
						
							|  |  |  | 			HomePath: "../../", | 
					
						
							|  |  |  | 			Args:     []string{"cfg:auth.proxy.sync_ttl=2"}, | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		require.Nil(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-01 18:31:06 +08:00
										 |  |  | 		require.Equal(t, 2, cfg.AuthProxy.SyncTTL) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("Test reading string values from .ini file", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		err := cfg.Load(CommandLineArgs{HomePath: "../../"}) | 
					
						
							|  |  |  | 		require.Nil(t, err) | 
					
						
							|  |  |  | 		iniFile, err := ini.Load(path.Join(cfg.HomePath, "pkg/setting/testdata/invalid.ini")) | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		require.Nil(t, err) | 
					
						
							| 
									
										
										
										
											2019-04-25 14:29:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		t.Run("If key is found - should return value from ini file", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-09-08 17:33:04 +08:00
										 |  |  | 			value := valueAsString(iniFile.Section("server"), "alt_url", "") | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Equal(t, "https://grafana.com/", value) | 
					
						
							| 
									
										
										
										
											2019-04-25 14:29:07 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 		t.Run("If key is not found - should return default value", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-09-08 17:33:04 +08:00
										 |  |  | 			value := valueAsString(iniFile.Section("server"), "extra_url", "default_url_val") | 
					
						
							| 
									
										
										
										
											2021-10-26 19:19:07 +08:00
										 |  |  | 			require.Equal(t, "default_url_val", value) | 
					
						
							| 
									
										
										
										
											2019-04-25 14:29:07 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
											  
											
												Add a separate grafana.com API URL setting (#59506)
The GrafanaComURL setting is currently used in two places:
- the /api/gnet endpoint, which proxies all requests to the URL
  configured in GrafanaComURL
- OAuth logins using grafana.com, where the auth URL, token URL and
  redirect URL are all configured to use the GrafanaComURL.
This has worked fine until now because almost all Grafana instances have
just used the default value, https://grafana.com. However, we now have a
few different grafana.com's, some of which are behind IAP. The IAP
causes the /api/gnet proxy to fail because the required cookies are not
present in the request (how could they be?). Setting the
[grafana_net.url] setting to an internal-only URL improves the situation
slightly - the proxy works again just fine - but breaks any OAuth logins
using grafana.com, because the user must be redirected to a publicly
accessible URL.
This commit adds an additional setting, `[grafana_com.api_url]`,
which can be used to tell Grafana to use the new API URL when proxying
requests to the grafana.com API, while still using the existing
`GrafanaComURL` setting for other things.
The setting will fall back to the GrafanaComURL setting + "/api" if unset.
											
										 
											2022-12-02 01:06:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("grafana.com API URL can be set separately from grafana.com URL", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2023-06-05 17:31:03 +08:00
										 |  |  | 		t.Setenv("GF_GRAFANA_NET_URL", "https://grafana-dev.com") | 
					
						
							|  |  |  | 		t.Setenv("GF_GRAFANA_COM_API_URL", "http://grafana-dev.internal/api") | 
					
						
							| 
									
										
											  
											
												Add a separate grafana.com API URL setting (#59506)
The GrafanaComURL setting is currently used in two places:
- the /api/gnet endpoint, which proxies all requests to the URL
  configured in GrafanaComURL
- OAuth logins using grafana.com, where the auth URL, token URL and
  redirect URL are all configured to use the GrafanaComURL.
This has worked fine until now because almost all Grafana instances have
just used the default value, https://grafana.com. However, we now have a
few different grafana.com's, some of which are behind IAP. The IAP
causes the /api/gnet proxy to fail because the required cookies are not
present in the request (how could they be?). Setting the
[grafana_net.url] setting to an internal-only URL improves the situation
slightly - the proxy works again just fine - but breaks any OAuth logins
using grafana.com, because the user must be redirected to a publicly
accessible URL.
This commit adds an additional setting, `[grafana_com.api_url]`,
which can be used to tell Grafana to use the new API URL when proxying
requests to the grafana.com API, while still using the existing
`GrafanaComURL` setting for other things.
The setting will fall back to the GrafanaComURL setting + "/api" if unset.
											
										 
											2022-12-02 01:06:12 +08:00
										 |  |  | 		cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2023-06-05 17:31:03 +08:00
										 |  |  | 		err := cfg.Load(CommandLineArgs{HomePath: "../../", Config: "../../conf/defaults.ini"}) | 
					
						
							| 
									
										
											  
											
												Add a separate grafana.com API URL setting (#59506)
The GrafanaComURL setting is currently used in two places:
- the /api/gnet endpoint, which proxies all requests to the URL
  configured in GrafanaComURL
- OAuth logins using grafana.com, where the auth URL, token URL and
  redirect URL are all configured to use the GrafanaComURL.
This has worked fine until now because almost all Grafana instances have
just used the default value, https://grafana.com. However, we now have a
few different grafana.com's, some of which are behind IAP. The IAP
causes the /api/gnet proxy to fail because the required cookies are not
present in the request (how could they be?). Setting the
[grafana_net.url] setting to an internal-only URL improves the situation
slightly - the proxy works again just fine - but breaks any OAuth logins
using grafana.com, because the user must be redirected to a publicly
accessible URL.
This commit adds an additional setting, `[grafana_com.api_url]`,
which can be used to tell Grafana to use the new API URL when proxying
requests to the grafana.com API, while still using the existing
`GrafanaComURL` setting for other things.
The setting will fall back to the GrafanaComURL setting + "/api" if unset.
											
										 
											2022-12-02 01:06:12 +08:00
										 |  |  | 		require.Nil(t, err) | 
					
						
							|  |  |  | 		require.Equal(t, "https://grafana-dev.com", cfg.GrafanaComURL) | 
					
						
							|  |  |  | 		require.Equal(t, "http://grafana-dev.internal/api", cfg.GrafanaComAPIURL) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("grafana.com API URL falls back to grafana.com URL + /api", func(t *testing.T) { | 
					
						
							|  |  |  | 		err := os.Unsetenv("GF_GRAFANA_NET_URL") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		err = os.Unsetenv("GF_GRAFANA_COM_API_URL") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		err = cfg.Load(CommandLineArgs{HomePath: "../../"}) | 
					
						
							|  |  |  | 		require.Nil(t, err) | 
					
						
							|  |  |  | 		require.Equal(t, "https://grafana.com", cfg.GrafanaComURL) | 
					
						
							|  |  |  | 		require.Equal(t, "https://grafana.com/api", cfg.GrafanaComAPIURL) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2015-02-12 18:55:55 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-03-11 17:04:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 02:26:19 +08:00
										 |  |  | func TestParseAppURLAndSubURL(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-03-11 17:04:48 +08:00
										 |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		rootURL           string | 
					
						
							|  |  |  | 		expectedAppURL    string | 
					
						
							|  |  |  | 		expectedAppSubURL string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{rootURL: "http://localhost:3000/", expectedAppURL: "http://localhost:3000/"}, | 
					
						
							|  |  |  | 		{rootURL: "http://localhost:3000", expectedAppURL: "http://localhost:3000/"}, | 
					
						
							|  |  |  | 		{rootURL: "http://localhost:3000/grafana", expectedAppURL: "http://localhost:3000/grafana/", expectedAppSubURL: "/grafana"}, | 
					
						
							|  |  |  | 		{rootURL: "http://localhost:3000/grafana/", expectedAppURL: "http://localhost:3000/grafana/", expectedAppSubURL: "/grafana"}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tc := range testCases { | 
					
						
							|  |  |  | 		f := ini.Empty() | 
					
						
							| 
									
										
										
										
											2021-11-09 00:56:56 +08:00
										 |  |  | 		cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2020-03-11 17:04:48 +08:00
										 |  |  | 		s, err := f.NewSection("server") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		_, err = s.NewKey("root_url", tc.rootURL) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-11-09 00:56:56 +08:00
										 |  |  | 		appURL, appSubURL, err := cfg.parseAppUrlAndSubUrl(s) | 
					
						
							| 
									
										
										
										
											2020-03-11 17:04:48 +08:00
										 |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		require.Equal(t, tc.expectedAppURL, appURL) | 
					
						
							|  |  |  | 		require.Equal(t, tc.expectedAppSubURL, appSubURL) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-11-03 02:26:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 21:57:38 +08:00
										 |  |  | func TestAuthDurationSettings(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-11-03 02:26:19 +08:00
										 |  |  | 	const maxInactiveDaysTest = 240 * time.Hour | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-14 21:57:38 +08:00
										 |  |  | 	f := ini.Empty() | 
					
						
							|  |  |  | 	cfg := NewCfg() | 
					
						
							|  |  |  | 	sec, err := f.NewSection("auth") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-06-01 18:29:15 +08:00
										 |  |  | 	_, err = sec.NewKey("login_maximum_inactive_lifetime_duration", "10d") | 
					
						
							| 
									
										
										
										
											2020-09-14 21:57:38 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	err = readAuthSettings(f, cfg) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, maxInactiveDaysTest, cfg.LoginMaxInactiveLifetime) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	f = ini.Empty() | 
					
						
							|  |  |  | 	sec, err = f.NewSection("auth") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	_, err = sec.NewKey("login_maximum_inactive_lifetime_duration", "824h") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2020-11-03 02:26:19 +08:00
										 |  |  | 	maxInactiveDurationTest, err := time.ParseDuration("824h") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2020-09-14 21:57:38 +08:00
										 |  |  | 	err = readAuthSettings(f, cfg) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, maxInactiveDurationTest, cfg.LoginMaxInactiveLifetime) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	f = ini.Empty() | 
					
						
							|  |  |  | 	sec, err = f.NewSection("auth") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2022-06-01 18:29:15 +08:00
										 |  |  | 	_, err = sec.NewKey("login_maximum_lifetime_duration", "24d") | 
					
						
							| 
									
										
										
										
											2020-09-14 21:57:38 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2020-11-03 02:26:19 +08:00
										 |  |  | 	maxLifetimeDaysTest, err := time.ParseDuration("576h") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2020-09-14 21:57:38 +08:00
										 |  |  | 	err = readAuthSettings(f, cfg) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, maxLifetimeDaysTest, cfg.LoginMaxLifetime) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	f = ini.Empty() | 
					
						
							|  |  |  | 	sec, err = f.NewSection("auth") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	_, err = sec.NewKey("login_maximum_lifetime_duration", "824h") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2020-11-03 02:26:19 +08:00
										 |  |  | 	maxLifetimeDurationTest, err := time.ParseDuration("824h") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2020-09-14 21:57:38 +08:00
										 |  |  | 	err = readAuthSettings(f, cfg) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, maxLifetimeDurationTest, cfg.LoginMaxLifetime) | 
					
						
							| 
									
										
										
										
											2021-01-07 15:58:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	f = ini.Empty() | 
					
						
							|  |  |  | 	sec, err = f.NewSection("auth") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	_, err = sec.NewKey("login_maximum_lifetime_duration", "") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	maxLifetimeDurationTest, err = time.ParseDuration("720h") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	err = readAuthSettings(f, cfg) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, maxLifetimeDurationTest, cfg.LoginMaxLifetime) | 
					
						
							| 
									
										
										
										
											2020-09-14 21:57:38 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-02-01 17:13:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestGetCDNPath(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2023-12-07 04:27:08 +08:00
										 |  |  | 	t.Run("should return CDN url as expected", func(t *testing.T) { | 
					
						
							|  |  |  | 		var ( | 
					
						
							|  |  |  | 			err    error | 
					
						
							|  |  |  | 			actual string | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		cfg.BuildVersion = "v7.5.0-11124" | 
					
						
							|  |  |  | 		cfg.CDNRootURL, err = url.Parse("http://cdn.grafana.com") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-02-01 17:13:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-07 04:27:08 +08:00
										 |  |  | 		actual, err = cfg.GetContentDeliveryURL("grafana-oss") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		require.Equal(t, "http://cdn.grafana.com/grafana-oss/v7.5.0-11124/", actual) | 
					
						
							|  |  |  | 		actual, err = cfg.GetContentDeliveryURL("grafana") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		require.Equal(t, "http://cdn.grafana.com/grafana/v7.5.0-11124/", actual) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-02-02 19:34:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-07 04:27:08 +08:00
										 |  |  | 	t.Run("should error if BuildVersion  is not set", func(t *testing.T) { | 
					
						
							|  |  |  | 		var err error | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		cfg.CDNRootURL, err = url.Parse("http://cdn.grafana.com") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_, err = cfg.GetContentDeliveryURL("grafana") | 
					
						
							|  |  |  | 		require.Error(t, err) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-02-01 17:13:09 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestGetCDNPathWithPreReleaseVersionAndSubPath(t *testing.T) { | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	cfg := NewCfg() | 
					
						
							|  |  |  | 	cfg.BuildVersion = "v7.5.0-11124pre" | 
					
						
							|  |  |  | 	cfg.CDNRootURL, err = url.Parse("http://cdn.grafana.com/sub") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2023-12-07 04:27:08 +08:00
										 |  |  | 	actual, err := cfg.GetContentDeliveryURL("grafana-oss") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, "http://cdn.grafana.com/sub/grafana-oss/v7.5.0-11124pre/", actual) | 
					
						
							|  |  |  | 	actual, err = cfg.GetContentDeliveryURL("grafana") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, "http://cdn.grafana.com/sub/grafana/v7.5.0-11124pre/", actual) | 
					
						
							| 
									
										
										
										
											2021-02-01 17:13:09 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Adding a case for this in case we switch to proper semver version strings
 | 
					
						
							|  |  |  | func TestGetCDNPathWithAlphaVersion(t *testing.T) { | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	cfg := NewCfg() | 
					
						
							|  |  |  | 	cfg.BuildVersion = "v7.5.0-alpha.11124" | 
					
						
							|  |  |  | 	cfg.CDNRootURL, err = url.Parse("http://cdn.grafana.com") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2023-12-07 04:27:08 +08:00
										 |  |  | 	actual, err := cfg.GetContentDeliveryURL("grafana-oss") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, "http://cdn.grafana.com/grafana-oss/v7.5.0-alpha.11124/", actual) | 
					
						
							|  |  |  | 	actual, err = cfg.GetContentDeliveryURL("grafana") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.Equal(t, "http://cdn.grafana.com/grafana/v7.5.0-alpha.11124/", actual) | 
					
						
							| 
									
										
										
										
											2021-02-01 17:13:09 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-09-29 22:16:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestAlertingEnabled(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2024-03-08 05:01:11 +08:00
										 |  |  | 	t.Run("fail if legacy alerting enabled", func(t *testing.T) { | 
					
						
							|  |  |  | 		f := ini.Empty() | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2021-11-30 03:51:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 05:01:11 +08:00
										 |  |  | 		alertingSec, err := f.NewSection("alerting") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		_, err = alertingSec.NewKey("enabled", "true") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							| 
									
										
										
										
											2021-09-29 22:16:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 05:01:11 +08:00
										 |  |  | 		require.Error(t, cfg.readAlertingSettings(f)) | 
					
						
							| 
									
										
										
										
											2021-11-30 03:51:15 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 05:01:11 +08:00
										 |  |  | 	t.Run("do nothing if it is disabled", func(t *testing.T) { | 
					
						
							|  |  |  | 		f := ini.Empty() | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2021-09-29 22:16:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 05:01:11 +08:00
										 |  |  | 		alertingSec, err := f.NewSection("alerting") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		_, err = alertingSec.NewKey("enabled", "false") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		require.NoError(t, cfg.readAlertingSettings(f)) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-09-29 22:16:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 05:01:11 +08:00
										 |  |  | 	t.Run("do nothing if it invalid", func(t *testing.T) { | 
					
						
							|  |  |  | 		f := ini.Empty() | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							| 
									
										
										
										
											2021-09-29 22:16:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 05:01:11 +08:00
										 |  |  | 		alertingSec, err := f.NewSection("alerting") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		_, err = alertingSec.NewKey("enabled", "test") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		require.NoError(t, cfg.readAlertingSettings(f)) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-09-29 22:16:40 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-05-10 18:30:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestRedactedValue(t *testing.T) { | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		desc     string | 
					
						
							|  |  |  | 		key      string | 
					
						
							|  |  |  | 		value    string | 
					
						
							|  |  |  | 		expected string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			desc:     "non-sensitive key", | 
					
						
							|  |  |  | 			key:      "admin_user", | 
					
						
							|  |  |  | 			value:    "admin", | 
					
						
							|  |  |  | 			expected: "admin", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			desc:     "sensitive key with non-empty value", | 
					
						
							|  |  |  | 			key:      "private_key_path", | 
					
						
							|  |  |  | 			value:    "/path/to/key", | 
					
						
							|  |  |  | 			expected: RedactedPassword, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2024-02-02 01:37:36 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			desc:     "license key with non-empty value", | 
					
						
							|  |  |  | 			key:      "GF_ENTERPRISE_LICENSE_TEXT", | 
					
						
							|  |  |  | 			value:    "some_license_key_test", | 
					
						
							|  |  |  | 			expected: RedactedPassword, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-05-10 18:30:50 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			desc:     "sensitive key with empty value", | 
					
						
							|  |  |  | 			key:      "private_key_path", | 
					
						
							|  |  |  | 			value:    "", | 
					
						
							|  |  |  | 			expected: "", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2024-12-04 23:58:29 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			desc:     "authentication_token", | 
					
						
							|  |  |  | 			key:      "my_authentication_token", | 
					
						
							|  |  |  | 			value:    "test", | 
					
						
							|  |  |  | 			expected: RedactedPassword, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-05-10 18:30:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tc := range testCases { | 
					
						
							|  |  |  | 		t.Run(tc.desc, func(t *testing.T) { | 
					
						
							|  |  |  | 			require.Equal(t, tc.expected, RedactedValue(tc.key, tc.value)) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-07-14 19:04:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestHandleAWSSettings(t *testing.T) { | 
					
						
							|  |  |  | 	t.Run("Should set default auth providers if not defined", func(t *testing.T) { | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		awsSection, err := cfg.Raw.NewSection("aws") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		_, err = awsSection.NewKey("allowed_auth_providers", "") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cfg.handleAWSConfig() | 
					
						
							|  |  |  | 		assert.Equal(t, []string{"default", "keys", "credentials"}, cfg.AWSAllowedAuthProviders) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	t.Run("Should pass on auth providers defined in config", func(t *testing.T) { | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		awsSection, err := cfg.Raw.NewSection("aws") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		_, err = awsSection.NewKey("allowed_auth_providers", "keys, credentials") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cfg.handleAWSConfig() | 
					
						
							|  |  |  | 		assert.Equal(t, []string{"keys", "credentials"}, cfg.AWSAllowedAuthProviders) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	t.Run("Should set assume role to true if not defined", func(t *testing.T) { | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		awsSection, err := cfg.Raw.NewSection("aws") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		_, err = awsSection.NewKey("assume_role_enabled", "") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cfg.handleAWSConfig() | 
					
						
							|  |  |  | 		assert.Equal(t, true, cfg.AWSAssumeRoleEnabled) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	t.Run("Should set assume role to true if defined as true in the config", func(t *testing.T) { | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		awsSection, err := cfg.Raw.NewSection("aws") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		_, err = awsSection.NewKey("assume_role_enabled", "true") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cfg.handleAWSConfig() | 
					
						
							|  |  |  | 		assert.Equal(t, true, cfg.AWSAssumeRoleEnabled) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	t.Run("Should set assume role to false if defined as false in the config", func(t *testing.T) { | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		awsSection, err := cfg.Raw.NewSection("aws") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		_, err = awsSection.NewKey("assume_role_enabled", "false") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cfg.handleAWSConfig() | 
					
						
							|  |  |  | 		assert.Equal(t, false, cfg.AWSAssumeRoleEnabled) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	t.Run("Should set default page limit if not defined", func(t *testing.T) { | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		awsSection, err := cfg.Raw.NewSection("aws") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		_, err = awsSection.NewKey("list_metrics_page_limit", "") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cfg.handleAWSConfig() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		assert.Equal(t, 500, cfg.AWSListMetricsPageLimit) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	t.Run("Should pass on the limit if it is defined in the config", func(t *testing.T) { | 
					
						
							|  |  |  | 		cfg := NewCfg() | 
					
						
							|  |  |  | 		awsSection, err := cfg.Raw.NewSection("aws") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 		_, err = awsSection.NewKey("list_metrics_page_limit", "400") | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cfg.handleAWSConfig() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		assert.Equal(t, 400, cfg.AWSListMetricsPageLimit) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-01-23 19:36:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const iniString = ` | 
					
						
							|  |  |  | app_mode = production | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [server] | 
					
						
							|  |  |  | domain = test.com | 
					
						
							|  |  |  | ` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestNewCfgFromBytes(t *testing.T) { | 
					
						
							|  |  |  | 	cfg, err := NewCfgFromBytes([]byte(iniString)) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.NotNil(t, cfg) | 
					
						
							|  |  |  | 	require.Equal(t, Prod, cfg.Env) | 
					
						
							|  |  |  | 	require.Equal(t, "test.com", cfg.Domain) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestNewCfgFromINIFile(t *testing.T) { | 
					
						
							|  |  |  | 	parsedFile, err := ini.Load([]byte(iniString)) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.NotNil(t, parsedFile) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cfg, err := NewCfgFromINIFile(parsedFile) | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 	require.NotNil(t, cfg) | 
					
						
							|  |  |  | 	require.Equal(t, Prod, cfg.Env) | 
					
						
							|  |  |  | 	require.Equal(t, "test.com", cfg.Domain) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-02-05 23:25:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestDynamicSection(t *testing.T) { | 
					
						
							|  |  |  | 	t.Parallel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("repro #44509 - panic on concurrent map write", func(t *testing.T) { | 
					
						
							|  |  |  | 		t.Parallel() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const ( | 
					
						
							|  |  |  | 			goroutines = 10 | 
					
						
							|  |  |  | 			attempts   = 1000 | 
					
						
							|  |  |  | 			section    = "DEFAULT" | 
					
						
							|  |  |  | 			key        = "TestDynamicSection_repro_44509" | 
					
						
							|  |  |  | 			value      = "theval" | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cfg, err := NewCfgFromBytes([]byte(``)) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ds := &DynamicSection{ | 
					
						
							|  |  |  | 			section: cfg.Raw.Section(section), | 
					
						
							|  |  |  | 			Logger:  log.NewNopLogger(), | 
					
						
							|  |  |  | 			env:     osutil.MapEnv{}, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		osVar := EnvKey(section, key) | 
					
						
							|  |  |  | 		err = ds.env.Setenv(osVar, value) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var wg sync.WaitGroup | 
					
						
							|  |  |  | 		for i := 0; i < goroutines; i++ { | 
					
						
							|  |  |  | 			wg.Add(1) | 
					
						
							|  |  |  | 			go require.NotPanics(t, func() { | 
					
						
							|  |  |  | 				for i := 0; i < attempts; i++ { | 
					
						
							|  |  |  | 					ds.section.Key(key).SetValue("") | 
					
						
							|  |  |  | 					ds.Key(key) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				wg.Done() | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		wg.Wait() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		assert.Equal(t, value, ds.section.Key(key).String()) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } |