...

Source file src/edge-infra.dev/pkg/edge/api/types/config.go

Documentation: edge-infra.dev/pkg/edge/api/types

     1  package types
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"k8s.io/client-go/util/homedir"
    10  
    11  	"edge-infra.dev/pkg/edge/api/bsl/types"
    12  )
    13  
    14  // Program arguments / Environment variables
    15  const (
    16  	// app
    17  	appEnv           = "app-env"
    18  	appSecret        = "app-secret"
    19  	totpSecret       = "totp-secret-key"
    20  	kubeconfig       = "kubeconfig"
    21  	containerCluster = "container-cluster"
    22  	containerPool    = "container-pool"
    23  	ParentCluster    = "parent-cluster"
    24  
    25  	// test
    26  	testBffEndpoint = "bff-endpoint"
    27  	testProject     = "test-project"
    28  	testCluster     = "test-cluster"
    29  	testStore       = "test-store"
    30  	testTenant      = "test-tenant"
    31  	testBannerEu    = "test-banner-eu"
    32  	testBannerOrg   = "test-banner-org"
    33  	testUsername    = "test-username"
    34  	testPassword    = "test-password"
    35  	testK8Version   = "test-k8-version"
    36  
    37  	// bsp
    38  	bspEndpoint           = "bsp-endpoint"
    39  	bspRoot               = "bsp-root"
    40  	bspSiteEndpoint       = "bsp-site-endpoint"
    41  	bspOrganizationPrefix = "bsp-organization-prefix"
    42  	bspResetURL           = "bsp-reset-url"
    43  
    44  	// chariot
    45  	chariotEndpoint    = "chariot-endpoint"
    46  	chariotPubsubTopic = "chariot-pubsub-topic"
    47  
    48  	// bff
    49  	topLevelProjectID = "top-level-project-id"
    50  	gcpRegion         = "gcp-region"
    51  	gcpZone           = "gcp-zone"
    52  	BFFUsername       = "bffuser"
    53  
    54  	// big query
    55  	bigQueryTable   = "bq-table"
    56  	psBigQueryTable = "ps-bq-table"
    57  
    58  	// launch darkly
    59  	launchDarkly  = "ld-key"
    60  	ldUseBigQuery = "ld-bigquery-flag"
    61  	ldUsePubSub   = "ld-pubsub-flag"
    62  
    63  	// error format for missing program args
    64  	errorFormat = "missing program argument `%s` or environment variable `%s`"
    65  
    66  	//sql db
    67  	sqlConnectionName = "sql-connection-name"
    68  	sqlHostName       = "sql-host-name"
    69  	sqlPort           = "sql-port"
    70  	sqlSearchPath     = "sql-search-path"
    71  	sqlUser           = "sql-user"
    72  	sqlPassword       = "sql-password"
    73  	sqlDatabaseName   = "sql-db-name"
    74  
    75  	// defaults
    76  	containerClusterLocation = "files/containerCluster.yaml"
    77  	containerPoolLocation    = "files/containerPool.yaml"
    78  	defaultTopLevelProject   = "ret-edge-dev0-foreman"
    79  	defaultK8Version         = "1.21.4-gke.2300"
    80  	BigQueryTableName        = "ret-edge-dev0-foreman.edge.latest-resources"
    81  	BigQueryPSTableName      = "ret-edge-dev0-foreman.ctlfishpubsub.resources"
    82  	ldUseBigQueryFlag        = "bigquery-test"
    83  	ldUsePubSubFlag          = "pubsub-test"
    84  	defaultGCPRegion         = "us-east1"
    85  	defaultGCPZone           = "b"
    86  
    87  	//test defaults
    88  	defaultTestBanner = "4082649e-e28a-47d1-973f-3aae63cd9123"
    89  	defaultTestOrg    = "integration-testing-org"
    90  
    91  	//edge api endpoint
    92  	edgeAPIURL = "edge-api-url"
    93  
    94  	//okta issuer
    95  	oktaIssuer = "okta-issuer"
    96  	//okta client id
    97  	oktaClientID = "okta-client-id"
    98  
    99  	// edge security compliance default
   100  	edgeOptInSecurityCompliance = "edge-sec-opt-in-compliance"
   101  	edgeMaxLeaseValidityPeriod  = "edge-sec-max-lease-period"
   102  	edgeMaxValidityPeriod       = "edge-sec-max-validity-period"
   103  
   104  	// ui config
   105  	gaTrackingID = "ga-tracking-id"
   106  )
   107  
   108  type Config struct {
   109  	App                         AppConfig
   110  	BSP                         types.BSPConfig
   111  	BSPSiteURI                  types.BSPConfig
   112  	Chariot                     ChariotConfig
   113  	Bff                         BffConfig
   114  	BigQuery                    BigQueryConfig
   115  	LaunchDarkly                LaunchDarklyConfig
   116  	SQL                         SQLConfig
   117  	EdgeAPIEndpoint             string
   118  	Okta                        OktaConfig
   119  	EdgeOptInSecurityCompliance bool
   120  	EdgeMaxLeaseValidityPeriod  string
   121  	EdgeMaxSecretValidityPeriod string
   122  	UI                          UIConfig
   123  }
   124  
   125  type AppConfig struct {
   126  	Environment              string
   127  	AppSecret                string
   128  	TotpSecret               string
   129  	KubeConfig               string
   130  	ContainerClusterLocation string
   131  	ContainerPoolLocation    string
   132  }
   133  
   134  type OktaConfig struct {
   135  	OktaIssuer string
   136  	ClientID   string
   137  }
   138  
   139  type TestConfig struct {
   140  	TestBffEndpoint string
   141  	TestProject     string
   142  	TestCluster     string
   143  	TestStore       string
   144  	TestTenant      string
   145  	TestBannerEU    string
   146  	TestBannerOrg   string
   147  	TestUsername    string
   148  	TestPassword    string
   149  	TestK8Version   string
   150  	Config          Config
   151  }
   152  
   153  type ChariotConfig struct {
   154  	PubsubTopic string
   155  }
   156  
   157  type BffConfig struct {
   158  	TopLevelProjectID string
   159  	GCPRegion         string
   160  	GCPZone           string
   161  }
   162  
   163  type BigQueryConfig struct {
   164  	TableName   string
   165  	PSTableName string
   166  }
   167  
   168  type LaunchDarklyConfig struct {
   169  	SDKKey       string
   170  	BigQueryFlag string
   171  	PubSubFlag   string
   172  }
   173  
   174  type SQLConfig struct {
   175  	ConnectionName string
   176  	Host           string
   177  	Port           string
   178  	SearchPath     string
   179  	User           string
   180  	Password       string
   181  	DatabaseName   string
   182  }
   183  
   184  type UIConfig struct {
   185  	GaTrackingID string
   186  }
   187  
   188  func (config *TestConfig) BindFlags(flags *flag.FlagSet) {
   189  	config.Config.BindFlags(flags)
   190  
   191  	flags.StringVar(
   192  		&config.TestBffEndpoint,
   193  		testBffEndpoint,
   194  		config.TestBffEndpoint,
   195  		"bff api endpoint",
   196  	)
   197  
   198  	flags.StringVar(
   199  		&config.TestProject,
   200  		testProject,
   201  		config.TestProject,
   202  		"bff test project",
   203  	)
   204  
   205  	flags.StringVar(
   206  		&config.TestCluster,
   207  		testCluster,
   208  		config.TestCluster,
   209  		"bff test cluster",
   210  	)
   211  
   212  	flags.StringVar(
   213  		&config.TestStore,
   214  		testStore,
   215  		config.TestStore,
   216  		"bff test store",
   217  	)
   218  
   219  	flags.StringVar(
   220  		&config.TestTenant,
   221  		testTenant,
   222  		defaultTestOrg,
   223  		"bff test tenant",
   224  	)
   225  
   226  	flags.StringVar(
   227  		&config.TestBannerEU,
   228  		testBannerEu,
   229  		defaultTestBanner,
   230  		"bff test banner eu",
   231  	)
   232  
   233  	flags.StringVar(
   234  		&config.TestBannerOrg,
   235  		testBannerOrg,
   236  		config.TestBannerOrg,
   237  		"bff test banner org",
   238  	)
   239  
   240  	flags.StringVar(
   241  		&config.TestUsername,
   242  		testUsername,
   243  		config.TestUsername,
   244  		"bff test username",
   245  	)
   246  
   247  	flags.StringVar(
   248  		&config.TestPassword,
   249  		testPassword,
   250  		config.TestPassword,
   251  		"bff test password",
   252  	)
   253  
   254  	flags.StringVar(
   255  		&config.TestK8Version,
   256  		testK8Version,
   257  		defaultK8Version,
   258  		"bff test kubernetes version",
   259  	)
   260  }
   261  
   262  // BindKubeConfigFlag binds the kubeconfig flag separately to avoid collision with controller-runtime flags
   263  // https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/client/config/config.go
   264  func (config *Config) BindKubeConfigFlag(flags *flag.FlagSet) {
   265  	if home := homedir.HomeDir(); home != "" {
   266  		flags.StringVar(
   267  			&config.App.KubeConfig,
   268  			kubeconfig,
   269  			filepath.Join(home, ".kube", "config"),
   270  			"(optional) absolute path to the kubeconfig file",
   271  		)
   272  	} else {
   273  		flags.StringVar(
   274  			&config.App.KubeConfig,
   275  			kubeconfig,
   276  			config.App.KubeConfig,
   277  			"absolute path to the kubeconfig file",
   278  		)
   279  	}
   280  }
   281  
   282  func (config *Config) BindFlags(flags *flag.FlagSet) {
   283  	// App Config
   284  	flags.StringVar(
   285  		&config.App.Environment,
   286  		appEnv,
   287  		config.App.Environment,
   288  		"environment in which app is running dev/prod",
   289  	)
   290  
   291  	flags.StringVar(
   292  		&config.App.AppSecret,
   293  		appSecret,
   294  		config.App.AppSecret,
   295  		"jwt secret for signing token",
   296  	)
   297  
   298  	flags.StringVar(
   299  		&config.App.TotpSecret,
   300  		totpSecret,
   301  		config.App.TotpSecret,
   302  		"totp secret for validating totp token",
   303  	)
   304  
   305  	flags.StringVar(
   306  		&config.App.ContainerClusterLocation,
   307  		containerCluster,
   308  		containerClusterLocation,
   309  		"location for containerCLuster config",
   310  	)
   311  
   312  	flags.StringVar(
   313  		&config.App.ContainerPoolLocation,
   314  		containerPool,
   315  		containerPoolLocation,
   316  		"location for containerPool config",
   317  	)
   318  
   319  	// BSP Services Config
   320  	flags.StringVar(
   321  		&config.BSP.Endpoint,
   322  		bspEndpoint,
   323  		config.BSP.Endpoint,
   324  		"bsp url",
   325  	)
   326  
   327  	// BSP Services Site Config
   328  	// Needed because the url for the bsl site api is different
   329  	flags.StringVar(
   330  		&config.BSPSiteURI.Endpoint,
   331  		bspSiteEndpoint,
   332  		config.BSPSiteURI.Endpoint,
   333  		"bsp site url",
   334  	)
   335  
   336  	flags.StringVar(
   337  		&config.BSP.Root,
   338  		bspRoot,
   339  		config.BSP.Root,
   340  		"edge bsp bspRoot org",
   341  	)
   342  
   343  	flags.StringVar(
   344  		&config.BSP.OrganizationPrefix,
   345  		bspOrganizationPrefix,
   346  		config.BSP.OrganizationPrefix,
   347  		"edge bsp organization prefix",
   348  	)
   349  
   350  	flags.StringVar(
   351  		&config.BSP.ResetURL,
   352  		bspResetURL,
   353  		config.BSP.ResetURL,
   354  		"edge bsp resetUrl",
   355  	)
   356  
   357  	// Chariot Pubsub Topic Config
   358  	flags.StringVar(
   359  		&config.Chariot.PubsubTopic,
   360  		chariotPubsubTopic,
   361  		config.Chariot.PubsubTopic,
   362  		"chariot pubsub topic",
   363  	)
   364  
   365  	// Bff Config
   366  	flags.StringVar(
   367  		&config.Bff.TopLevelProjectID,
   368  		topLevelProjectID,
   369  		defaultTopLevelProject,
   370  		"top level project id",
   371  	)
   372  
   373  	flags.StringVar(
   374  		&config.Bff.GCPRegion,
   375  		gcpRegion,
   376  		defaultGCPRegion,
   377  		"gcp region",
   378  	)
   379  
   380  	flags.StringVar(
   381  		&config.Bff.GCPZone,
   382  		gcpZone,
   383  		defaultGCPZone,
   384  		"gcp zone",
   385  	)
   386  
   387  	// Big Query Config
   388  	flags.StringVar(
   389  		&config.BigQuery.TableName,
   390  		bigQueryTable,
   391  		BigQueryTableName,
   392  		"big query table name",
   393  	)
   394  	flags.StringVar(
   395  		&config.BigQuery.PSTableName,
   396  		psBigQueryTable,
   397  		BigQueryPSTableName,
   398  		"pubsub big query table name",
   399  	)
   400  
   401  	// LaunchDarkly Config
   402  	flags.StringVar(
   403  		&config.LaunchDarkly.SDKKey,
   404  		launchDarkly,
   405  		config.LaunchDarkly.SDKKey,
   406  		"launch darkly sdk key",
   407  	)
   408  
   409  	flags.StringVar(
   410  		&config.LaunchDarkly.BigQueryFlag,
   411  		ldUseBigQuery,
   412  		ldUseBigQueryFlag,
   413  		"launch darkly big query flag",
   414  	)
   415  
   416  	flags.StringVar(
   417  		&config.LaunchDarkly.PubSubFlag,
   418  		ldUsePubSub,
   419  		ldUsePubSubFlag,
   420  		"launch darkly pubsub flag",
   421  	)
   422  
   423  	//sql config
   424  	flags.StringVar(
   425  		&config.SQL.ConnectionName,
   426  		sqlConnectionName,
   427  		config.SQL.ConnectionName,
   428  		"sql connection name",
   429  	)
   430  
   431  	flags.StringVar(
   432  		&config.SQL.Host,
   433  		sqlHostName,
   434  		config.SQL.Host,
   435  		"sql host name",
   436  	)
   437  
   438  	flags.StringVar(
   439  		&config.SQL.Port,
   440  		sqlPort,
   441  		config.SQL.Port,
   442  		"sql port number",
   443  	)
   444  
   445  	flags.StringVar(
   446  		&config.SQL.SearchPath,
   447  		sqlSearchPath,
   448  		config.SQL.SearchPath,
   449  		"sql connection name",
   450  	)
   451  
   452  	flags.StringVar(
   453  		&config.SQL.User,
   454  		sqlUser,
   455  		config.SQL.User,
   456  		"sql db user",
   457  	)
   458  
   459  	flags.StringVar(
   460  		&config.SQL.Password,
   461  		sqlPassword,
   462  		config.SQL.Password,
   463  		"sql db password",
   464  	)
   465  
   466  	flags.StringVar(
   467  		&config.SQL.DatabaseName,
   468  		sqlDatabaseName,
   469  		config.SQL.DatabaseName,
   470  		"sql db name",
   471  	)
   472  
   473  	flags.StringVar(
   474  		&config.EdgeAPIEndpoint,
   475  		edgeAPIURL,
   476  		config.EdgeAPIEndpoint,
   477  		"edge api env url",
   478  	)
   479  
   480  	// okta config
   481  	flags.StringVar(
   482  		&config.Okta.OktaIssuer,
   483  		oktaIssuer,
   484  		config.Okta.OktaIssuer,
   485  		"okta issuer",
   486  	)
   487  	flags.StringVar(
   488  		&config.Okta.ClientID,
   489  		oktaClientID,
   490  		config.Okta.ClientID,
   491  		"okta client id",
   492  	)
   493  
   494  	flags.BoolVar(
   495  		&config.EdgeOptInSecurityCompliance,
   496  		edgeOptInSecurityCompliance,
   497  		false,
   498  		"edge security compliance default setting",
   499  	)
   500  
   501  	flags.StringVar(
   502  		&config.EdgeMaxLeaseValidityPeriod,
   503  		edgeMaxLeaseValidityPeriod,
   504  		"48h",
   505  		"edge security max lease validity period in duration i.e. 48h",
   506  	)
   507  
   508  	flags.StringVar(
   509  		&config.EdgeMaxSecretValidityPeriod,
   510  		edgeMaxValidityPeriod,
   511  		"60d",
   512  		"edge security max secret validity period in duration i.e. 60d",
   513  	)
   514  	// ui config
   515  	flags.StringVar(
   516  		&config.UI.GaTrackingID,
   517  		gaTrackingID,
   518  		config.UI.GaTrackingID,
   519  		"ui ga tracking ID",
   520  	)
   521  }
   522  
   523  func (config *Config) Validate() {
   524  	// Validate required app config
   525  	validateArgNotEmpty(config.App.Environment, appEnv)
   526  	validateArgNotEmpty(config.App.AppSecret, appSecret)
   527  
   528  	// Validate required bsp config
   529  	validateArgNotEmpty(config.BSP.Endpoint, bspEndpoint)
   530  	validateArgNotEmpty(config.BSPSiteURI.Endpoint, bspSiteEndpoint)
   531  
   532  	// Validate required bff config
   533  	validateArgNotEmpty(config.Bff.TopLevelProjectID, topLevelProjectID)
   534  	validateArgNotEmpty(config.Bff.GCPRegion, gcpRegion)
   535  	validateArgNotEmpty(config.Bff.GCPZone, gcpZone)
   536  	// Validate okta issuer is not empty
   537  	validateArgNotEmpty(config.Okta.OktaIssuer, oktaIssuer)
   538  	// Validate okta client is not empty
   539  	validateArgNotEmpty(config.Okta.ClientID, oktaClientID)
   540  }
   541  
   542  func validateArgNotEmpty(arg, argName string) {
   543  	if arg == "" {
   544  		panic(fmt.Errorf(errorFormat, argName, toEnvironmentName(argName)))
   545  	}
   546  }
   547  
   548  func toEnvironmentName(arg string) string {
   549  	return strings.ReplaceAll(strings.ToUpper(arg), "-", "_")
   550  }
   551  

View as plain text