github.com/nats-io/nsc/v2@v2.8.7-0.20240307184528-efd7023c6896/cmd/wellknownoperator.go (about) 1 /* 2 * Copyright 2018-2022 The NATS Authors 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package cmd 17 18 import ( 19 "net/url" 20 "os" 21 "strings" 22 ) 23 24 const EnvOperatorPrefix = "NSC_" 25 const EnvOperatorSuffix = "_OPERATOR" 26 27 type KnownOperator struct { 28 Name string `json:"name"` 29 URL string `json:"url"` 30 AccountServerURL string `json:"account_server_url"` 31 } 32 33 type KnownOperators []KnownOperator 34 35 var wellKnownOperators KnownOperators 36 37 func defaultWellKnownOperators() KnownOperators { 38 return KnownOperators{ 39 {Name: "synadia", URL: "https://www.synadia.com", AccountServerURL: "https://api.synadia.io/jwt/v1/operator"}, 40 } 41 } 42 43 func GetWellKnownOperators() (KnownOperators, error) { 44 if wellKnownOperators == nil { 45 wellKnownOperators = defaultWellKnownOperators() 46 eo := fromEnv() 47 if eo != nil { 48 wellKnownOperators = append(wellKnownOperators, eo...) 49 } 50 } 51 return wellKnownOperators, nil 52 } 53 54 // fromEnv returns operators in the environment named as `NSC_<name>_OPERATOR` 55 // the value on the environment should be an URL 56 func fromEnv() KnownOperators { 57 return findEnvOperators(os.Environ()) 58 } 59 60 func findEnvOperators(env []string) KnownOperators { 61 pl := len(EnvOperatorPrefix) 62 sl := len(EnvOperatorSuffix) 63 var envOps KnownOperators 64 for _, v := range env { 65 pair := strings.Split(v, "=") 66 k := strings.ToUpper(pair[0]) 67 if strings.HasPrefix(k, EnvOperatorPrefix) && strings.HasSuffix(k, EnvOperatorSuffix) { 68 var envOp KnownOperator 69 envOp.Name = pair[0][pl : len(pair[0])-sl] 70 envOp.AccountServerURL = pair[1] 71 envOps = append(envOps, envOp) 72 } 73 74 } 75 return envOps 76 } 77 78 func FindKnownOperator(name string) (*KnownOperator, error) { 79 a, err := GetWellKnownOperators() 80 if err != nil { 81 return nil, err 82 } 83 name = strings.ToLower(name) 84 for _, v := range a { 85 n := strings.ToLower(v.Name) 86 if n == name { 87 c := v 88 return &c, nil 89 } 90 } 91 return nil, nil 92 } 93 94 func GetOperatorName(name string, asu string) string { 95 uv, err := url.Parse(asu) 96 if err != nil { 97 return name 98 } 99 hn := strings.ToLower(uv.Hostname()) 100 ops, err := GetWellKnownOperators() 101 if err != nil { 102 return name 103 } 104 for _, o := range ops { 105 tu, err := url.Parse(o.AccountServerURL) 106 if err == nil { 107 h := strings.ToLower(tu.Hostname()) 108 if h == hn { 109 return o.Name 110 } 111 } 112 } 113 return name 114 115 }