Best Testkube code snippet using config.getPath
tls_helper.go
Source:tls_helper.go
1/*2 * Copyright 2017-2018 Dgraph Labs, Inc. and Contributors3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package x17import (18 "crypto/tls"19 "crypto/x509"20 "io/ioutil"21 "strings"22 "github.com/outcaste-io/ristretto/z"23 "github.com/pkg/errors"24 "github.com/spf13/pflag"25 "github.com/spf13/viper"26)27// TLSHelperConfig define params used to create a tls.Config28type TLSHelperConfig struct {29 CertRequired bool30 Cert string31 Key string32 ServerName string33 RootCACert string34 ClientAuth string35 UseSystemCACerts bool36}37const (38 TLSDefaults = `use-system-ca=true; client-auth-type=VERIFYIFGIVEN; internal-port=false; ` +39 `ca-cert=; server-name=; server-cert=; server-key=; client-cert=; client-key=;`40 TLSServerDefaults = `use-system-ca=true; client-auth-type=VERIFYIFGIVEN; internal-port=false; ` +41 `server-cert=; server-key=; ca-cert=; client-cert=; client-key=;`42 TLSClientDefaults = `use-system-ca=true; internal-port=false; server-name=; ca-cert=; ` +43 `client-cert=; client-key=;`44)45// RegisterServerTLSFlags registers the required flags to set up a TLS server.46func RegisterServerTLSFlags(flag *pflag.FlagSet) {47 flag.String("tls", "use-system-ca=true; client-auth-type=VERIFYIFGIVEN; internal-port=false;",48 z.NewSuperFlagHelp(TLSServerDefaults).49 Head("TLS Server options").50 Flag("internal-port",51 "(Optional) Enable inter-node TLS encryption between cluster nodes.").52 Flag("server-cert",53 "The server Cert file which is needed to initiate the server in the cluster.").54 Flag("server-key",55 "The server Key file which is needed to initiate the server in the cluster.").56 Flag("ca-cert",57 "The CA cert file used to verify server certificates. Required for enabling TLS.").58 Flag("use-system-ca",59 "Includes System CA into CA Certs.").60 Flag("client-auth-type",61 "The TLS client authentication method.").62 Flag("client-cert",63 "(Optional) The client Cert file which is needed to connect as a client with the other "+64 "nodes in the cluster.").65 Flag("client-key",66 "(Optional) The private client Key file which is needed to connect as a client with the "+67 "other nodes in the cluster.").68 String())69}70// RegisterClientTLSFlags registers the required flags to set up a TLS client.71func RegisterClientTLSFlags(flag *pflag.FlagSet) {72 flag.String("tls", "use-system-ca=true; internal-port=false;",73 z.NewSuperFlagHelp(TLSClientDefaults).74 Head("TLS Client options").75 Flag("internal-port",76 "(Optional) Enable inter-node TLS encryption between cluster nodes.").77 Flag("server-name",78 "Used to verify the server hostname.").79 Flag("ca-cert",80 "The CA cert file used to verify server certificates. Required for enabling TLS.").81 Flag("use-system-ca",82 "Includes System CA into CA Certs.").83 Flag("client-cert",84 "(Optional) The Cert file provided by the client to the server.").85 Flag("client-key",86 "(Optional) The private Key file provided by the clients to the server.").87 String())88}89// LoadClientTLSConfigForInternalPort loads tls config for connecting to internal ports of cluster90func LoadClientTLSConfigForInternalPort(v *viper.Viper) (*tls.Config, error) {91 tlsFlag := z.NewSuperFlag(v.GetString("tls")).MergeAndCheckDefault(TLSDefaults)92 if !tlsFlag.GetBool("internal-port") {93 return nil, nil94 }95 if tlsFlag.GetPath("client-cert") == "" || tlsFlag.GetPath("client-key") == "" {96 return nil, errors.Errorf(`Inter-node TLS is enabled but client certs are not provided. ` +97 `Inter-node TLS is always client authenticated. Please provide --tls ` +98 `"client-cert=...; client-key=...;"`)99 }100 conf := &TLSHelperConfig{}101 conf.UseSystemCACerts = tlsFlag.GetBool("use-system-ca")102 conf.RootCACert = tlsFlag.GetPath("ca-cert")103 conf.CertRequired = true104 conf.Cert = tlsFlag.GetPath("client-cert")105 conf.Key = tlsFlag.GetPath("client-key")106 return GenerateClientTLSConfig(conf)107}108// LoadServerTLSConfigForInternalPort loads the TLS config for the internal ports of the cluster109func LoadServerTLSConfigForInternalPort(v *viper.Viper) (*tls.Config, error) {110 tlsFlag := z.NewSuperFlag(v.GetString("tls")).MergeAndCheckDefault(TLSDefaults)111 if !tlsFlag.GetBool("internal-port") {112 return nil, nil113 }114 if tlsFlag.GetPath("server-cert") == "" || tlsFlag.GetPath("server-key") == "" {115 return nil, errors.Errorf(`Inter-node TLS is enabled but server node certs are not provided. ` +116 `Please provide --tls "server-cert=...; server-key=...;"`)117 }118 conf := TLSHelperConfig{}119 conf.UseSystemCACerts = tlsFlag.GetBool("use-system-ca")120 conf.RootCACert = tlsFlag.GetPath("ca-cert")121 conf.CertRequired = true122 conf.Cert = tlsFlag.GetPath("server-cert")123 conf.Key = tlsFlag.GetPath("server-key")124 conf.ClientAuth = "REQUIREANDVERIFY"125 return GenerateServerTLSConfig(&conf)126}127// LoadServerTLSConfig loads the TLS config into the server with the given parameters.128func LoadServerTLSConfig(v *viper.Viper) (*tls.Config, error) {129 tlsFlag := z.NewSuperFlag(v.GetString("tls")).MergeAndCheckDefault(TLSDefaults)130 if tlsFlag.GetPath("server-cert") == "" && tlsFlag.GetPath("server-key") == "" {131 return nil, nil132 }133 conf := TLSHelperConfig{}134 conf.RootCACert = tlsFlag.GetPath("ca-cert")135 conf.CertRequired = true136 conf.Cert = tlsFlag.GetPath("server-cert")137 conf.Key = tlsFlag.GetPath("server-key")138 conf.ClientAuth = tlsFlag.GetString("client-auth-type")139 conf.UseSystemCACerts = tlsFlag.GetBool("use-system-ca")140 return GenerateServerTLSConfig(&conf)141}142// SlashTLSConfig returns the TLS config appropriate for SlashGraphQL143// This assumes that endpoint is not empty, and in the format "domain.grpc.cloud.dg.io:443"144func SlashTLSConfig(endpoint string) (*tls.Config, error) {145 pool, err := generateCertPool("", true)146 if err != nil {147 return nil, err148 }149 hostWithoutPort := strings.Split(endpoint, ":")[0]150 return &tls.Config{151 RootCAs: pool,152 ServerName: hostWithoutPort,153 }, nil154}155// LoadClientTLSConfig loads the TLS config into the client with the given parameters.156func LoadClientTLSConfig(v *viper.Viper) (*tls.Config, error) {157 if v.GetString("slash_grpc_endpoint") != "" {158 return SlashTLSConfig(v.GetString("slash_grpc_endpoint"))159 }160 tlsFlag := z.NewSuperFlag(v.GetString("tls")).MergeAndCheckDefault(TLSDefaults)161 // When the --tls ca-cert="..."; option is specified, the connection will be set up using TLS162 // instead of plaintext. However the client cert files are optional, depending on whether the163 // server requires a client certificate.164 caCert := tlsFlag.GetPath("ca-cert")165 if caCert != "" {166 tlsCfg := tls.Config{}167 // 1. set up the root CA168 pool, err := generateCertPool(caCert, tlsFlag.GetBool("use-system-ca"))169 if err != nil {170 return nil, err171 }172 tlsCfg.RootCAs = pool173 // 2. set up the server name for verification174 tlsCfg.ServerName = tlsFlag.GetString("server-name")175 // 3. optionally load the client cert files176 certFile := tlsFlag.GetPath("client-cert")177 keyFile := tlsFlag.GetPath("client-key")178 if certFile != "" && keyFile != "" {179 cert, err := tls.LoadX509KeyPair(certFile, keyFile)180 if err != nil {181 return nil, err182 }183 tlsCfg.Certificates = []tls.Certificate{cert}184 }185 return &tlsCfg, nil186 } else187 // Attempt to determine if user specified *any* TLS option. Unfortunately and contrary to188 // Viper's own documentation, there's no way to tell whether an option value came from a189 // command-line option or a built-it default.190 if tlsFlag.GetString("server-name") != "" ||191 tlsFlag.GetPath("client-cert") != "" ||192 tlsFlag.GetPath("client-key") != "" {193 return nil, errors.Errorf(`--tls "ca-cert=...;" is required for enabling TLS`)194 }195 return nil, nil196}197func generateCertPool(certPath string, useSystemCA bool) (*x509.CertPool, error) {198 var pool *x509.CertPool199 if useSystemCA {200 var err error201 if pool, err = x509.SystemCertPool(); err != nil {202 return nil, err203 }204 } else {205 pool = x509.NewCertPool()206 }207 if len(certPath) > 0 {208 caFile, err := ioutil.ReadFile(certPath)209 if err != nil {210 return nil, err211 }212 if !pool.AppendCertsFromPEM(caFile) {213 return nil, errors.Errorf("error reading CA file %q", certPath)214 }215 }216 return pool, nil217}218func setupClientAuth(authType string) (tls.ClientAuthType, error) {219 auth := map[string]tls.ClientAuthType{220 "REQUEST": tls.RequestClientCert,221 "REQUIREANY": tls.RequireAnyClientCert,222 "VERIFYIFGIVEN": tls.VerifyClientCertIfGiven,223 "REQUIREANDVERIFY": tls.RequireAndVerifyClientCert,224 }225 if len(authType) > 0 {226 if v, has := auth[strings.ToUpper(authType)]; has {227 return v, nil228 }229 return tls.NoClientCert, errors.Errorf("Invalid client auth. Valid values " +230 "[REQUEST, REQUIREANY, VERIFYIFGIVEN, REQUIREANDVERIFY]")231 }232 return tls.NoClientCert, nil233}234// TLSBaseConfig returns a *tls.Config with the base set of security235// requirements (minimum TLS v1.2 and set of cipher suites)236func TLSBaseConfig() *tls.Config {237 tlsCfg := new(tls.Config)238 tlsCfg.MinVersion = tls.VersionTLS12239 tlsCfg.CipherSuites = []uint16{240 tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,241 tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,242 tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,243 tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,244 tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,245 tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,246 tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,247 tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,248 tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,249 tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,250 tls.TLS_RSA_WITH_AES_128_GCM_SHA256,251 tls.TLS_RSA_WITH_AES_256_GCM_SHA384,252 tls.TLS_RSA_WITH_AES_256_CBC_SHA,253 }254 return tlsCfg255}256// GenerateServerTLSConfig creates and returns a new *tls.Config with the257// configuration provided.258func GenerateServerTLSConfig(config *TLSHelperConfig) (tlsCfg *tls.Config, err error) {259 if config.CertRequired {260 tlsCfg = TLSBaseConfig()261 cert, err := tls.LoadX509KeyPair(config.Cert, config.Key)262 if err != nil {263 return nil, err264 }265 tlsCfg.Certificates = []tls.Certificate{cert}266 pool, err := generateCertPool(config.RootCACert, config.UseSystemCACerts)267 if err != nil {268 return nil, err269 }270 tlsCfg.ClientCAs = pool271 auth, err := setupClientAuth(config.ClientAuth)272 if err != nil {273 return nil, err274 }275 tlsCfg.ClientAuth = auth276 return tlsCfg, nil277 }278 return nil, nil279}280// GenerateClientTLSConfig creates and returns a new client side *tls.Config with the281// configuration provided.282func GenerateClientTLSConfig(config *TLSHelperConfig) (tlsCfg *tls.Config, err error) {283 if config.CertRequired {284 tlsCfg := tls.Config{}285 // 1. set up the root CA286 pool, err := generateCertPool(config.RootCACert, config.UseSystemCACerts)287 if err != nil {288 return nil, err289 }290 tlsCfg.RootCAs = pool291 // 2. set up the server name for verification292 tlsCfg.ServerName = config.ServerName293 // 3. optionally load the client cert files294 certFile := config.Cert295 keyFile := config.Key296 if certFile != "" && keyFile != "" {297 cert, err := tls.LoadX509KeyPair(certFile, keyFile)298 if err != nil {299 return nil, err300 }301 tlsCfg.Certificates = []tls.Certificate{cert}302 }303 return &tlsCfg, nil304 }305 return nil, nil306}...
local.go
Source:local.go
...26 storage.Register(storage.Local, l)27 })28 return l, nil29}30func (l *local) getPath(key string) string {31 key = storage.NormalizeKey(key)32 return filepath.Join(l.config.RootDir, key)33}34func (l *local) Put(key string, r io.Reader, dataLength int64) error {35 path := l.getPath(key)36 dir, _ := filepath.Split(path)37 if err := os.MkdirAll(dir, os.ModePerm); err != nil {38 return err39 }40 fd, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)41 if err != nil {42 if os.IsPermission(err) {43 return storage.FileNoPermissionErr44 }45 return err46 }47 defer fd.Close()48 _, err = io.Copy(fd, r)49 return err50}51func (l *local) PutFile(key string, localFile string) error {52 path := l.getPath(localFile)53 fd, fileInfo, err := storage.OpenAsReadOnly(path)54 if err != nil {55 return err56 }57 defer fd.Close()58 return l.Put(key, fd, fileInfo.Size())59}60func (l *local) Get(key string) (io.ReadCloser, error) {61 path := l.getPath(key)62 fd, _, err := storage.OpenAsReadOnly(path)63 if err != nil {64 return nil, err65 }66 return fd, nil67}68func (l *local) Rename(srcKey string, destKey string) error {69 srcPath := l.getPath(srcKey)70 ok, err := l.Exists(srcPath)71 if err != nil {72 return err73 }74 if !ok {75 return storage.FileNotFoundErr76 }77 destPath := l.getPath(destKey)78 dir, _ := filepath.Split(destPath)79 if err = os.MkdirAll(dir, os.ModePerm); err != nil {80 return err81 }82 return os.Rename(srcPath, destPath)83}84func (l *local) Copy(srcKey string, destKey string) error {85 srcPath := l.getPath(srcKey)86 srcFd, _, err := storage.OpenAsReadOnly(srcPath)87 if err != nil {88 return err89 }90 defer srcFd.Close()91 destPath := l.getPath(destKey)92 dir, _ := filepath.Split(destPath)93 if err = os.MkdirAll(dir, os.ModePerm); err != nil {94 return err95 }96 destFd, err := os.OpenFile(destPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)97 if err != nil {98 if os.IsPermission(err) {99 return storage.FileNoPermissionErr100 }101 return err102 }103 defer destFd.Close()104 _, err = io.Copy(destFd, srcFd)105 if err != nil {106 return err107 }108 return nil109}110func (l *local) Exists(key string) (bool, error) {111 path := l.getPath(key)112 _, err := os.Stat(path)113 if err != nil {114 if os.IsNotExist(err) {115 return false, nil116 }117 if os.IsPermission(err) {118 return false, storage.FileNoPermissionErr119 }120 return false, err121 }122 return true, nil123}124func (l *local) Size(key string) (int64, error) {125 path := l.getPath(key)126 fileInfo, err := os.Stat(path)127 if err != nil {128 if os.IsNotExist(err) {129 return 0, storage.FileNotFoundErr130 }131 if os.IsPermission(err) {132 return 0, storage.FileNoPermissionErr133 }134 return 0, err135 }136 return fileInfo.Size(), nil137}138func (l *local) Delete(key string) error {139 path := l.getPath(key)140 err := os.Remove(path)141 if err != nil {142 if os.IsNotExist(err) {143 return storage.FileNotFoundErr144 }145 if os.IsPermission(err) {146 return storage.FileNoPermissionErr147 }148 return err149 }150 return nil151}152func (l *local) Url(key string) string {153 return l.config.AppUrl + "/" + storage.NormalizeKey(key)...
getPath
Using AI Code Generation
1import (2func main() {3 conf, err := config.NewConfig("ini", "config.conf")4 if err != nil {5 fmt.Println(err)6 }7 fmt.Println(conf.String("path"))8}
getPath
Using AI Code Generation
1import (2func main() {3 path, err := os.Getwd()4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }
getPath
Using AI Code Generation
1import (2func main() {3 fmt.Println(config.GetPath())4}5import (6func main() {7 fmt.Println(config.GetPath())8}
getPath
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World")4}5import (6type Config struct {7}8func (c *Config) getPath() string {9}10func main() {11 fmt.Println("Hello World")12}13import (14func main() {15 fmt.Println("Hello World")16 c := config.Config{}17 fmt.Println(c.getPath())18}19 /usr/lib/go-1.6/src/config (from $GOROOT)20 ($GOPATH not set)
getPath
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World!")4 configObj.GetPath()5}6import (7func main() {8 fmt.Println("Hello World!")9 configObj.GetPath()10}11import "fmt"12type Config struct {13}14func (c Config) GetPath() {15 fmt.Println("Path: config/config.go")16}17import "fmt"18type Config struct {19}20func (c Config) GetPath() {21 fmt.Println("Path: config/config.go")22}23import "fmt"24type Config struct {25}26func (c Config) GetPath() {27 fmt.Println("Path: config/config.go")28}29import "fmt"30type Config struct {31}32func (c Config) GetPath() {33 fmt.Println("Path: config/config.go")34}35import "fmt"36type Config struct {37}38func (c Config) GetPath() {39 fmt.Println("Path: config/config.go")40}41import "fmt"42type Config struct {43}44func (c Config) GetPath() {45 fmt.Println("Path: config/config.go")46}47import "fmt"48type Config struct {49}50func (
getPath
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World")4}5I have a config class in my project, I want to use the getPath() method of config class in 2.go file. How can I do this?6import (7func main() {8 fmt.Println(config.getPath())9}
getPath
Using AI Code Generation
1import (2func main() {3 fmt.Println("Path:", config.GetPath())4}5import (6func main() {7 fmt.Println("Path:", config.Path)8}9import (10func main() {11 fmt.Println("Path:", config.GetPath())12}13import (14func main() {15 fmt.Println("Path:", config.GetPath())16}17import (18func main() {19 fmt.Println("Path:", config.GetPath())20}21import (22func main() {23 fmt.Println("Path:", config.GetPath())24}25import (26func main() {27 fmt.Println("Path:", config.GetPath())28}29import (30func main() {31 fmt.Println("Path:", config.GetPath())32}33import (34func main() {35 fmt.Println("Path:", config.GetPath())36}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!