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

[NET-477] Pick AMB URL dynamically #2489

Merged
merged 5 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
run: |
go vet ./...
go test -p 1 ./... -v
go test -p 1 ./ee -v --tags ee
env:
DATABASE: sqlite
CLIENT_MODE: "off"
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type ServerConfig struct {
NetworksLimit int `yaml:"network_limit"`
HostsLimit int `yaml:"host_limit"`
DeployedByOperator bool `yaml:"deployed_by_operator"`
Environment string `yaml:"environment"`
}

// SQLConfig - Generic SQL Config
Expand Down
13 changes: 12 additions & 1 deletion ee/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func validateLicenseKey(encryptedData []byte, publicKey *[32]byte) ([]byte, erro
return nil, err
}

req, err := http.NewRequest(http.MethodPost, api_endpoint, bytes.NewReader(requestBody))
req, err := http.NewRequest(http.MethodPost, getAccountsHost()+"/api/v1/license/validate", bytes.NewReader(requestBody))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -217,6 +217,17 @@ func validateLicenseKey(encryptedData []byte, publicKey *[32]byte) ([]byte, erro
return body, err
}

func getAccountsHost() string {
switch servercfg.GetEnvironment() {
case "dev":
return accountsHostDevelopment
case "staging":
return accountsHostStaging
default:
return accountsHostProduction
}
}

func cacheResponse(response []byte) error {
var lrc = licenseResponseCache{
Body: response,
Expand Down
77 changes: 77 additions & 0 deletions ee/license_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//go:build ee
// +build ee

package ee

import (
"github.com/gravitl/netmaker/config"
"testing"
)

func Test_getAccountsHost(t *testing.T) {
tests := []struct {
name string
envK string
envV string
conf string
want string
}{
{
name: "no env var and no conf",
envK: "NOT_THE_CORRECT_ENV_VAR",
envV: "dev",
want: "https://api.accounts.netmaker.io",
},
{
name: "dev env var",
envK: "ENVIRONMENT",
envV: "dev",
want: "https://api.dev.accounts.netmaker.io",
},
{
name: "staging env var",
envK: "ENVIRONMENT",
envV: "staging",
want: "https://api.staging.accounts.netmaker.io",
},
{
name: "prod env var",
envK: "ENVIRONMENT",
envV: "prod",
want: "https://api.accounts.netmaker.io",
},
{
name: "dev conf",
conf: "dev",
want: "https://api.dev.accounts.netmaker.io",
},
{
name: "staging conf",
conf: "staging",
want: "https://api.staging.accounts.netmaker.io",
},
{
name: "prod conf",
conf: "prod",
want: "https://api.accounts.netmaker.io",
},
{
name: "env var vs conf precedence",
envK: "ENVIRONMENT",
envV: "prod",
conf: "staging",
want: "https://api.accounts.netmaker.io",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.Config.Server.Environment = tt.conf
if tt.envK != "" {
t.Setenv(tt.envK, tt.envV)
}
if got := getAccountsHost(); got != tt.want {
t.Errorf("getAccountsHost() = %v, want %v", got, tt.want)
}
})
}
}
15 changes: 13 additions & 2 deletions ee/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package ee

import "fmt"
import (
"fmt"
)

// constants for accounts api hosts
const (
// accountsHostDevelopment is the accounts api host for development environment
accountsHostDevelopment = "https://api.dev.accounts.netmaker.io"
// accountsHostStaging is the accounts api host for staging environment
accountsHostStaging = "https://api.staging.accounts.netmaker.io"
// accountsHostProduction is the accounts api host for production environment
accountsHostProduction = "https://api.accounts.netmaker.io"
)

const (
api_endpoint = "https://api.accounts.netmaker.io/api/v1/license/validate"
license_cache_key = "license_response_cache"
license_validation_err_msg = "invalid license"
server_id_key = "nm-server-id"
Expand Down
11 changes: 11 additions & 0 deletions servercfg/serverconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,17 @@ func DeployedByOperator() bool {
return config.Config.Server.DeployedByOperator
}

// GetEnvironment returns the environment the server is running in (e.g. dev, staging, prod...)
func GetEnvironment() string {
if env := os.Getenv("ENVIRONMENT"); env != "" {
return env
}
if env := config.Config.Server.Environment; env != "" {
return env
}
return ""
}

// parseStunList - turn string into slice of StunServers
func parseStunList(stunString string) ([]models.StunServer, error) {
var err error
Expand Down