github.com/cs3org/reva/v2@v2.27.7/pkg/sharedconf/sharedconf.go (about) 1 // Copyright 2018-2021 CERN 2 // 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 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package sharedconf 20 21 import ( 22 "fmt" 23 "os" 24 "sync" 25 26 "github.com/mitchellh/mapstructure" 27 ) 28 29 var ( 30 sharedConf = &conf{} 31 sharedConfOnce sync.Once 32 ) 33 34 // ClientOptions represent additional options (e.g. tls settings) for the grpc clients 35 type ClientOptions struct { 36 TLSMode string `mapstructure:"tls_mode"` 37 CACertFile string `mapstructure:"cacert"` 38 } 39 40 type conf struct { 41 JWTSecret string `mapstructure:"jwt_secret"` 42 GatewaySVC string `mapstructure:"gatewaysvc"` 43 DataGateway string `mapstructure:"datagateway"` 44 SkipUserGroupsInToken bool `mapstructure:"skip_user_groups_in_token"` 45 GRPCClientOptions ClientOptions `mapstructure:"grpc_client_options"` 46 } 47 48 // Decode decodes the configuration. 49 func Decode(v interface{}) error { 50 var err error 51 52 sharedConfOnce.Do(func() { 53 if err = mapstructure.Decode(v, sharedConf); err != nil { 54 return 55 } 56 // add some defaults 57 if sharedConf.GatewaySVC == "" { 58 sharedConf.GatewaySVC = "0.0.0.0:19000" 59 } 60 61 // this is the default address we use for the data gateway HTTP service 62 if sharedConf.DataGateway == "" { 63 host, err := os.Hostname() 64 if err != nil || host == "" { 65 sharedConf.DataGateway = "http://0.0.0.0:19001/datagateway" 66 } else { 67 sharedConf.DataGateway = fmt.Sprintf("http://%s:19001/datagateway", host) 68 } 69 } 70 71 // TODO(labkode): would be cool to autogenerate one secret and print 72 // it on init time. 73 if sharedConf.JWTSecret == "" { 74 sharedConf.JWTSecret = "changemeplease" 75 } 76 }) 77 78 return err 79 } 80 81 // GetJWTSecret returns the package level configured jwt secret if not overwritten. 82 func GetJWTSecret(val string) string { 83 if val == "" { 84 return sharedConf.JWTSecret 85 } 86 return val 87 } 88 89 // GetGatewaySVC returns the package level configured gateway service if not overwritten. 90 func GetGatewaySVC(val string) string { 91 if val == "" { 92 return sharedConf.GatewaySVC 93 } 94 return val 95 } 96 97 // GetDataGateway returns the package level data gateway endpoint if not overwritten. 98 func GetDataGateway(val string) string { 99 if val == "" { 100 return sharedConf.DataGateway 101 } 102 return val 103 } 104 105 // SkipUserGroupsInToken returns whether to skip encoding user groups in the access tokens. 106 func SkipUserGroupsInToken() bool { 107 return sharedConf.SkipUserGroupsInToken 108 } 109 110 // GRPCClientOptions returns the global grpc client options 111 func GRPCClientOptions() ClientOptions { 112 return sharedConf.GRPCClientOptions 113 } 114 115 // this is used by the tests 116 func resetOnce() { 117 sharedConf = &conf{} 118 sharedConfOnce = sync.Once{} 119 }