github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/pkg/platform/api/settings.go (about)

     1  package api
     2  
     3  import (
     4  	"net/url"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/ActiveState/cli/pkg/projectfile"
     9  
    10  	"github.com/ActiveState/cli/internal/condition"
    11  	"github.com/ActiveState/cli/internal/constants"
    12  	"github.com/ActiveState/cli/internal/logging"
    13  )
    14  
    15  // Service records available api services
    16  type Service string
    17  
    18  const (
    19  	// ServiceMono is our main service for api services, "Mono" refers to its monolithic nature, one that we're trying to get away from
    20  	ServiceMono Service = "platform"
    21  
    22  	// ServiceSecrets is our service that's used purely for setting and storing secrets
    23  	ServiceSecrets = "secrets"
    24  
    25  	// ServiceHeadChef is our service that's used to kick off and track builds
    26  	ServiceHeadChef = "headchef"
    27  
    28  	// ServiceHeadChefWS is the websocket service on headchef
    29  	BuildLogStreamer = "buildlog-streamer"
    30  
    31  	// ServiceInventory is our service that's used to query available inventory and dependencies
    32  	ServiceInventory = "inventory"
    33  
    34  	// ServiceGraphQL is our service that's used as a graphql endpoint for platform requests
    35  	ServiceGraphQL = "platform-graphql"
    36  
    37  	// ServiceMediator is our mediator service used to query build graph data
    38  	ServiceMediator = "mediator"
    39  
    40  	// ServiceRequirementsImport is our service that processes requirements.txt files.
    41  	ServiceRequirementsImport = "requirements-import"
    42  
    43  	// ServiceBuildPlanner is our service that processes build plans.
    44  	ServiceBuildPlanner = "build-planner"
    45  
    46  	// ServiceVulnerabilities is Data Acquisition's Hasura service for vulnerability (CVE) information.
    47  	ServiceVulnerabilities = "vulnerabilities"
    48  
    49  	// ServiceHasuraInventory is the Hasura service for inventory information.
    50  	ServiceHasuraInventory = "hasura-inventory"
    51  
    52  	// TestingPlatform is the API host used by tests so-as not to affect production.
    53  	TestingPlatform = ".testing.tld"
    54  )
    55  
    56  var urlsByService = map[Service]*url.URL{
    57  	ServiceMono: {
    58  		Scheme: "https",
    59  		Host:   constants.DefaultAPIHost,
    60  		Path:   constants.MonoAPIPath,
    61  	},
    62  	ServiceSecrets: {
    63  		Scheme: "https",
    64  		Host:   constants.DefaultAPIHost,
    65  		Path:   constants.SecretsAPIPath,
    66  	},
    67  	ServiceHeadChef: {
    68  		Scheme: "https",
    69  		Host:   constants.DefaultAPIHost,
    70  		Path:   constants.HeadChefAPIPath,
    71  	},
    72  	BuildLogStreamer: {
    73  		Scheme: "wss",
    74  		Host:   constants.DefaultAPIHost,
    75  		Path:   constants.BuildLogStreamerPath,
    76  	},
    77  	ServiceInventory: {
    78  		Scheme: "https",
    79  		Host:   constants.DefaultAPIHost,
    80  		Path:   constants.InventoryAPIPath,
    81  	},
    82  	ServiceGraphQL: {
    83  		Scheme: "https",
    84  		Host:   constants.DefaultAPIHost,
    85  		Path:   constants.GraphqlAPIPath,
    86  	},
    87  	ServiceMediator: {
    88  		Scheme: "https",
    89  		Host:   constants.DefaultAPIHost,
    90  		Path:   constants.MediatorAPIPath,
    91  	},
    92  	ServiceRequirementsImport: {
    93  		Scheme: "https",
    94  		Host:   constants.DefaultAPIHost,
    95  		Path:   constants.RequirementsImportAPIPath,
    96  	},
    97  	ServiceBuildPlanner: {
    98  		Scheme: "https",
    99  		Host:   constants.DefaultAPIHost,
   100  		Path:   constants.BuildPlannerAPIPath,
   101  	},
   102  	ServiceVulnerabilities: {
   103  		Scheme: "https",
   104  		Host:   constants.DefaultAPIHost,
   105  		Path:   constants.VulnerabilitiesAPIPath,
   106  	},
   107  	ServiceHasuraInventory: {
   108  		Scheme: "https",
   109  		Host:   constants.DefaultAPIHost,
   110  		Path:   constants.HasuraInventoryAPIPath,
   111  	},
   112  }
   113  
   114  // GetServiceURL returns the URL for the given service
   115  func GetServiceURL(service Service) *url.URL {
   116  	serviceURL, validService := urlsByService[service]
   117  	if !validService {
   118  		logging.Panic("Invalid service: %s", string(service))
   119  	}
   120  	if host := getProjectHost(service); host != nil {
   121  		serviceURL.Host = *host
   122  	}
   123  
   124  	if insecure := os.Getenv(constants.APIInsecureEnvVarName); insecure == "true" {
   125  		if serviceURL.Scheme == "https" || serviceURL.Scheme == "wss" {
   126  			serviceURL.Scheme = strings.TrimRight(serviceURL.Scheme, "s")
   127  		}
   128  	}
   129  
   130  	sname := strings.Replace(strings.ToUpper(string(service)), "-", "_", -1)
   131  	envname := constants.APIServiceOverrideEnvVarName + sname
   132  	if override := os.Getenv(envname); override != "" {
   133  		u, err := url.Parse(override)
   134  		if err != nil {
   135  			logging.Error("Could not apply %s: %s", envname, err)
   136  		} else {
   137  			return u
   138  		}
   139  	}
   140  
   141  	return serviceURL
   142  }
   143  
   144  func getProjectHost(service Service) *string {
   145  	if apiHost := os.Getenv(constants.APIHostEnvVarName); apiHost != "" {
   146  		return &apiHost
   147  	}
   148  
   149  	if condition.InUnitTest() {
   150  		testingPlatform := string(service) + TestingPlatform
   151  		return &testingPlatform
   152  	}
   153  
   154  	pj, err := projectfile.GetOnce()
   155  	if err != nil {
   156  		return nil
   157  	}
   158  	url, err := url.Parse(pj.Project)
   159  	if err != nil {
   160  		logging.Error("Could not parse project url: %s", pj.Project)
   161  		return nil
   162  	}
   163  
   164  	return &url.Host
   165  }
   166  
   167  // GetPlatformURL returns a generic Platform URL for the given path.
   168  // This is for retrieving non-service URLs (e.g. signup URL).
   169  func GetPlatformURL(path string) *url.URL {
   170  	host := constants.DefaultAPIHost
   171  	if hostOverride := os.Getenv(constants.APIHostEnvVarName); hostOverride != "" {
   172  		host = hostOverride
   173  	}
   174  	return &url.URL{
   175  		Scheme: "https",
   176  		Host:   host,
   177  		Path:   path,
   178  	}
   179  }