|
| 1 | +/* |
| 2 | + * Copyright (c) 2020-2024. Devtron Inc. |
| 3 | + * |
| 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 at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * 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 and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "github.com/caarlos0/env" |
| 23 | + "go.uber.org/zap" |
| 24 | + "golang.org/x/oauth2/google" |
| 25 | + "google.golang.org/api/option" |
| 26 | + "google.golang.org/api/sheets/v4" |
| 27 | +) |
| 28 | + |
| 29 | +// GoogleCloudConfig holds configuration for Google Cloud services (Sheets, Storage, etc.) |
| 30 | +type GoogleCloudConfig struct { |
| 31 | + ServiceAccountJSON string `env:"FEEDBACK_GCP_CREDENTIAL_FILE_JSON_DATA" envDefault:""` |
| 32 | + SpreadsheetID string `env:"GOOGLE_SPREADSHEET_ID" envDefault:""` |
| 33 | +} |
| 34 | + |
| 35 | +// GoogleSheetsClient provides access to Google Sheets API and service account credentials |
| 36 | +// The same service account can be used for Google Cloud Storage |
| 37 | +type GoogleSheetsClient struct { |
| 38 | + SheetsService *sheets.Service |
| 39 | + Config *GoogleCloudConfig |
| 40 | +} |
| 41 | + |
| 42 | +/* #nosec */ |
| 43 | +func NewGoogleSheetsClient(logger *zap.SugaredLogger, blobConfig *BlobConfigVariables) (*GoogleSheetsClient, error) { |
| 44 | + cfg := &GoogleCloudConfig{} |
| 45 | + err := env.Parse(cfg) |
| 46 | + if err != nil { |
| 47 | + logger.Errorw("error parsing google cloud config", "err", err) |
| 48 | + return &GoogleSheetsClient{}, err |
| 49 | + } |
| 50 | + |
| 51 | + // Fallback: If GOOGLE_SERVICE_ACCOUNT_JSON is not set, try using FEEDBACK_GCP_CREDENTIAL_FILE_JSON_DATA |
| 52 | + serviceAccountJSON := cfg.ServiceAccountJSON |
| 53 | + if serviceAccountJSON == "" && blobConfig != nil && blobConfig.FeedbackGcpCredentialFileJsonData != "" { |
| 54 | + serviceAccountJSON = blobConfig.FeedbackGcpCredentialFileJsonData |
| 55 | + logger.Info("Using FEEDBACK_GCP_CREDENTIAL_FILE_JSON_DATA for Google Sheets authentication") |
| 56 | + } |
| 57 | + |
| 58 | + // If service account JSON is not provided, return empty client |
| 59 | + if serviceAccountJSON == "" { |
| 60 | + logger.Warn("Google service account JSON not provided (neither GOOGLE_SERVICE_ACCOUNT_JSON nor FEEDBACK_GCP_CREDENTIAL_FILE_JSON_DATA), Google Sheets client will not be initialized") |
| 61 | + return &GoogleSheetsClient{ |
| 62 | + SheetsService: nil, |
| 63 | + Config: cfg, |
| 64 | + }, nil |
| 65 | + } |
| 66 | + |
| 67 | + // Store the actual service account JSON being used |
| 68 | + cfg.ServiceAccountJSON = serviceAccountJSON |
| 69 | + |
| 70 | + ctx := context.Background() |
| 71 | + |
| 72 | + // Parse the service account JSON |
| 73 | + credentials, err := google.CredentialsFromJSON(ctx, []byte(serviceAccountJSON), sheets.SpreadsheetsScope) |
| 74 | + if err != nil { |
| 75 | + logger.Errorw("error creating credentials from service account JSON", "err", err) |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + // Create the sheets service |
| 80 | + sheetsService, err := sheets.NewService(ctx, option.WithCredentials(credentials)) |
| 81 | + if err != nil { |
| 82 | + logger.Errorw("error creating sheets service", "err", err) |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + logger.Info("Google Sheets client initialized successfully") |
| 87 | + |
| 88 | + return &GoogleSheetsClient{ |
| 89 | + SheetsService: sheetsService, |
| 90 | + Config: cfg, |
| 91 | + }, nil |
| 92 | +} |
| 93 | + |
| 94 | +// IsConfigured returns true if the Google Sheets client is properly configured |
| 95 | +func (c *GoogleSheetsClient) IsConfigured() bool { |
| 96 | + if c == nil { |
| 97 | + return false |
| 98 | + } |
| 99 | + return c.SheetsService != nil && c.Config != nil && c.Config.SpreadsheetID != "" |
| 100 | +} |
| 101 | + |
| 102 | +// GetSpreadsheetID returns the configured spreadsheet ID |
| 103 | +func (c *GoogleSheetsClient) GetSpreadsheetID() string { |
| 104 | + if c == nil || c.Config == nil { |
| 105 | + return "" |
| 106 | + } |
| 107 | + return c.Config.SpreadsheetID |
| 108 | +} |
| 109 | + |
| 110 | +// GetServiceAccountJSON returns the service account JSON for use with other Google Cloud services |
| 111 | +func (c *GoogleSheetsClient) GetServiceAccountJSON() string { |
| 112 | + if c == nil || c.Config == nil { |
| 113 | + return "" |
| 114 | + } |
| 115 | + return c.Config.ServiceAccountJSON |
| 116 | +} |
| 117 | + |
| 118 | +// MarshalServiceAccountJSON is a helper to convert service account key to JSON string |
| 119 | +func MarshalServiceAccountJSON(serviceAccountKey map[string]interface{}) (string, error) { |
| 120 | + jsonBytes, err := json.Marshal(serviceAccountKey) |
| 121 | + if err != nil { |
| 122 | + return "", err |
| 123 | + } |
| 124 | + return string(jsonBytes), nil |
| 125 | +} |
0 commit comments