-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(google_drive): Load Google oauth2 creds from Vault
- Loading branch information
1 parent
a4d18d8
commit ec2103c
Showing
4 changed files
with
103 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
utils/vault_google_drive_utils/vault_google_drive_utils.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package vault_google_drive_utils | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sikalabs/slu/config" | ||
"github.com/sikalabs/slu/utils/vault_utils" | ||
) | ||
|
||
func GetGoogleDriveUploadSecrets(vaultPath string) (string, string, error) { | ||
conf := config.ReadConfig() | ||
sec := config.ReadSecrets() | ||
|
||
client, err := vault_utils.GetClient(conf.SluVault.Url) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
client.SetToken(sec.SluVault.Token) | ||
|
||
secret, err := client.Logical().Read(vaultPath) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
if secret == nil { | ||
return "", "", fmt.Errorf("secret " + vaultPath + " not found") | ||
} | ||
data, ok := secret.Data["data"].(map[string]interface{}) | ||
if !ok { | ||
return "", "", fmt.Errorf("wrong data") | ||
} | ||
clientId, err := getString(data, "CLIENT_ID", true) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
clientSecret, err := getString(data, "CLIENT_SECRET", true) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
return clientId, clientSecret, nil | ||
} | ||
|
||
func getString(data map[string]interface{}, key string, required bool) (string, error) { | ||
val, ok := data[key] | ||
if !ok { | ||
if !required { | ||
return "", nil | ||
} | ||
return "", fmt.Errorf("key \"%s\" not found", key) | ||
} | ||
if val == nil { | ||
if !required { | ||
return "", nil | ||
} | ||
return "", fmt.Errorf("no value for key \"%s\"", key) | ||
} | ||
return val.(string), nil | ||
} |