-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: read db password from aws secrect manager (#12)
- Loading branch information
1 parent
19a4968
commit 998a310
Showing
4 changed files
with
107 additions
and
6 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
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,66 @@ | ||
package util | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/secretsmanager" | ||
) | ||
|
||
func GetSecret(secretName, region string) (string, error) { | ||
// Create a Secrets Manager client | ||
sess, err := session.NewSession(&aws.Config{ | ||
Region: ®ion, | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
svc := secretsmanager.New(sess) | ||
input := &secretsmanager.GetSecretValueInput{ | ||
SecretId: aws.String(secretName), | ||
VersionStage: aws.String("AWSCURRENT"), // VersionStage defaults to AWSCURRENT if unspecified | ||
} | ||
|
||
result, err := svc.GetSecretValue(input) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var secretString, decodedBinarySecret string | ||
if result.SecretString != nil { | ||
secretString = *result.SecretString | ||
return secretString, nil | ||
} else { | ||
decodedBinarySecretBytes := make([]byte, base64.StdEncoding.DecodedLen(len(result.SecretBinary))) | ||
length, err := base64.StdEncoding.Decode(decodedBinarySecretBytes, result.SecretBinary) | ||
if err != nil { | ||
fmt.Println("Base64 Decode Error:", err) | ||
return "", err | ||
} | ||
decodedBinarySecret = string(decodedBinarySecretBytes[:length]) | ||
return decodedBinarySecret, nil | ||
} | ||
} | ||
|
||
func SendTelegramMessage(identity string, botId string, chatId string, msg string) { | ||
if botId == "" || chatId == "" || msg == "" { | ||
return | ||
} | ||
|
||
endPoint := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", botId) | ||
formData := url.Values{ | ||
"chat_id": {chatId}, | ||
"parse_mode": {"html"}, | ||
"text": {fmt.Sprintf("%s: %s", identity, msg)}, | ||
} | ||
_, err := http.PostForm(endPoint, formData) | ||
if err != nil { | ||
fmt.Printf("send telegram message error, bot_id=%s, chat_id=%s, msg=%s, err=%s \n", botId, chatId, msg, err.Error()) | ||
return | ||
} | ||
} |