|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "crypto/x509" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "time" |
| 11 | + |
| 12 | + harness "github.com/harness/ff-golang-server-sdk/client" |
| 13 | + "github.com/harness/ff-golang-server-sdk/evaluation" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + flagName string = getEnvOrDefault("FF_FLAG_NAME", "harnessappdemodarkmode") |
| 18 | + sdkKey string = getEnvOrDefault("FF_API_KEY", "change me") |
| 19 | +) |
| 20 | + |
| 21 | +func main() { |
| 22 | + log.Println("Harness SDK TLS Example") |
| 23 | + |
| 24 | + certPool, err := loadCertificates([]string{"path to PEM", "path to PEM"}) |
| 25 | + if err != nil { |
| 26 | + log.Printf("Failed to parse PEM files: `%s`\n", err) |
| 27 | + } |
| 28 | + |
| 29 | + // Create a custom TLS configuration and use the CA pool. |
| 30 | + tlsConfig := &tls.Config{ |
| 31 | + RootCAs: certPool, |
| 32 | + } |
| 33 | + |
| 34 | + transport := &http.Transport{ |
| 35 | + TLSClientConfig: tlsConfig, |
| 36 | + } |
| 37 | + |
| 38 | + httpClient := http.Client{Transport: transport} |
| 39 | + |
| 40 | + // Create a feature flag client and wait for it to successfully initialize |
| 41 | + startTime := time.Now() |
| 42 | + |
| 43 | + // Note that this code uses ffserver hostname as an example, likely you'll have your own hostname or IP. |
| 44 | + // You should ensure the endpoint is returning a cert with valid SANs configured for the host/IP. |
| 45 | + client, err := harness.NewCfClient(sdkKey, harness.WithEventsURL("https://ffserver:8003/api/1.0"), harness.WithURL("https://ffserver:8003/api/1.0"), harness.WithWaitForInitialized(true), harness.WithHTTPClient(&httpClient)) |
| 46 | + |
| 47 | + elapsedTime := time.Since(startTime) |
| 48 | + log.Printf("Took '%v' seconds to get a client initialization result ", elapsedTime.Seconds()) |
| 49 | + |
| 50 | + if err != nil { |
| 51 | + log.Printf("Client failed to initialize: `%s`\n", err) |
| 52 | + } |
| 53 | + |
| 54 | + defer func() { |
| 55 | + err := client.Close() |
| 56 | + if err != nil { |
| 57 | + return |
| 58 | + } |
| 59 | + }() |
| 60 | + |
| 61 | + // Create a target (different targets can get different results based on rules) |
| 62 | + target := evaluation.Target{ |
| 63 | + Identifier: "HT_1", |
| 64 | + Name: "Harness_Target_1", |
| 65 | + Attributes: &map[ string] interface{}{ "email": "[email protected]"}, |
| 66 | + } |
| 67 | + |
| 68 | + // Loop forever reporting the state of the flag |
| 69 | + for { |
| 70 | + resultBool, err := client.BoolVariation(flagName, &target, false) |
| 71 | + if err != nil { |
| 72 | + log.Printf("failed to get evaluation: %v ", err) |
| 73 | + } |
| 74 | + log.Printf("Flag variation %v\n", resultBool) |
| 75 | + |
| 76 | + time.Sleep(10 * time.Second) |
| 77 | + } |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +func getEnvOrDefault(key, defaultStr string) string { |
| 82 | + value := os.Getenv(key) |
| 83 | + if value == "" { |
| 84 | + return defaultStr |
| 85 | + } |
| 86 | + return value |
| 87 | +} |
| 88 | + |
| 89 | +// Load certificates from PEM files |
| 90 | +func loadCertificates(filePaths []string) (*x509.CertPool, error) { |
| 91 | + pool := x509.NewCertPool() |
| 92 | + for _, ca := range filePaths { |
| 93 | + caBytes, err := os.ReadFile(ca) |
| 94 | + if err != nil { |
| 95 | + return nil, fmt.Errorf("failed to read CA certificate from file: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + if !pool.AppendCertsFromPEM(caBytes) { |
| 99 | + return nil, fmt.Errorf("could not append CA certificate") |
| 100 | + } |
| 101 | + } |
| 102 | + return pool, nil |
| 103 | +} |
0 commit comments