| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package gcp |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/plugin/agent/secrets/secretstore" |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/agent/secrets/secretstore/internal/httpx" |
| 12 | ) |
| 13 | |
| 14 | func (s *store) init(_ context.Context) error { |
| 15 | switch { |
| 16 | case s.Config.Timeout.Duration() < 0: |
| 17 | return fmt.Errorf("timeout cannot be negative") |
| 18 | case s.Config.Timeout.Duration() == 0: |
| 19 | s.Config.Timeout = defaultTimeout |
| 20 | } |
| 21 | s.runtime = &runtime{ |
| 22 | apiClient: httpx.APIClient(s.Config.Timeout.Duration()), |
| 23 | metadataClient: httpx.NoProxyClient(s.Config.Timeout.Duration()), |
| 24 | } |
| 25 | |
| 26 | published := &publishedStore{runtime: s.runtime} |
| 27 | |
| 28 | switch strings.TrimSpace(s.Config.Mode) { |
| 29 | case "metadata": |
| 30 | s.Config.Mode = "metadata" |
| 31 | s.Config.ModeServiceAccountFile = nil |
| 32 | published.mode = s.Config.Mode |
| 33 | case "service_account_file": |
| 34 | if s.Config.ModeServiceAccountFile == nil { |
| 35 | return fmt.Errorf("mode_service_account_file is required when mode is 'service_account_file'") |
| 36 | } |
| 37 | path := strings.TrimSpace(s.Config.ModeServiceAccountFile.Path) |
| 38 | if path == "" { |
| 39 | return fmt.Errorf("mode_service_account_file.path is required") |
| 40 | } |
| 41 | s.Config.Mode = "service_account_file" |
| 42 | s.Config.ModeServiceAccountFile.Path = path |
| 43 | published.mode = s.Config.Mode |
| 44 | published.serviceAccountFilePath = path |
| 45 | default: |
| 46 | return fmt.Errorf("mode '%s' is invalid for kind '%s'", s.Config.Mode, secretstore.KindGCPSM) |
| 47 | } |
| 48 | |
| 49 | s.published = published |
| 50 | return nil |
| 51 | } |