2022-02-26 21:48:03 +07:00
|
|
|
package main
|
|
|
|
|
2022-03-10 00:32:48 +07:00
|
|
|
// TODO WIP clean up
|
2022-03-01 02:18:45 +07:00
|
|
|
|
|
|
|
// TODO ACL policy
|
|
|
|
// path "secret/*" {
|
|
|
|
// capabilities = [
|
|
|
|
// "create",
|
|
|
|
// "list"
|
|
|
|
// ]
|
|
|
|
// }
|
|
|
|
|
2022-02-26 21:48:03 +07:00
|
|
|
import (
|
2022-03-03 02:37:11 +07:00
|
|
|
"fmt"
|
2022-02-26 21:48:03 +07:00
|
|
|
"log"
|
2022-03-09 01:05:51 +07:00
|
|
|
"os"
|
2022-02-26 21:48:03 +07:00
|
|
|
|
|
|
|
vault "github.com/hashicorp/vault/api"
|
2022-03-01 02:18:45 +07:00
|
|
|
"github.com/sethvargo/go-password/password"
|
2022-03-03 02:37:11 +07:00
|
|
|
"gopkg.in/yaml.v2"
|
2022-02-26 21:48:03 +07:00
|
|
|
)
|
|
|
|
|
2022-03-03 02:37:11 +07:00
|
|
|
type RandomPassword struct {
|
2022-03-09 01:00:24 +07:00
|
|
|
Path string
|
|
|
|
Data []struct {
|
|
|
|
Key string
|
|
|
|
Length int
|
|
|
|
Special bool
|
|
|
|
}
|
2022-03-03 02:37:11 +07:00
|
|
|
}
|
|
|
|
|
2022-02-26 21:48:03 +07:00
|
|
|
func main() {
|
2022-03-09 01:05:51 +07:00
|
|
|
data, err := os.ReadFile("./config.yaml")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to read config file: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-03-03 02:37:11 +07:00
|
|
|
randomPasswords := []RandomPassword{}
|
|
|
|
|
2022-03-09 01:05:51 +07:00
|
|
|
err = yaml.Unmarshal([]byte(data), &randomPasswords)
|
2022-03-03 02:37:11 +07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error: %v", err)
|
|
|
|
}
|
2022-02-26 21:48:03 +07:00
|
|
|
config := vault.DefaultConfig()
|
|
|
|
|
|
|
|
client, err := vault.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to initialize Vault client: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-03-03 02:37:11 +07:00
|
|
|
for _, randomPassword := range randomPasswords {
|
|
|
|
path := fmt.Sprintf("/secret/data/%s", randomPassword.Path)
|
2022-02-26 21:48:03 +07:00
|
|
|
|
2022-03-03 02:37:11 +07:00
|
|
|
secret, _ := client.Logical().Read(path)
|
2022-03-01 02:18:45 +07:00
|
|
|
|
2022-03-03 02:37:11 +07:00
|
|
|
if secret == nil {
|
2022-03-09 01:00:24 +07:00
|
|
|
secretData := map[string]interface{}{
|
|
|
|
"data": map[string]interface{}{},
|
2022-03-03 02:37:11 +07:00
|
|
|
}
|
2022-03-01 02:18:45 +07:00
|
|
|
|
2022-03-09 01:00:24 +07:00
|
|
|
for _, randomKey := range randomPassword.Data {
|
|
|
|
res, err := password.Generate(32, 3, 3, false, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secretData["data"].(map[string]interface{})[randomKey.Key] = res
|
2022-03-03 02:37:11 +07:00
|
|
|
}
|
2022-03-01 02:18:45 +07:00
|
|
|
|
2022-03-03 02:37:11 +07:00
|
|
|
_, err = client.Logical().Write(path, secretData)
|
2022-03-09 01:00:24 +07:00
|
|
|
|
2022-03-03 02:37:11 +07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to write secret: %v", err)
|
|
|
|
} else {
|
|
|
|
log.Println("Secret written successfully.")
|
|
|
|
}
|
2022-03-01 02:18:45 +07:00
|
|
|
} else {
|
2022-03-03 02:37:11 +07:00
|
|
|
log.Println("Key abc in secret already existed.")
|
2022-03-01 02:18:45 +07:00
|
|
|
}
|
2022-02-26 21:48:03 +07:00
|
|
|
}
|
|
|
|
}
|