|
| 1 | +package secretmanagerrotatesecret |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "secret-manager-rotate-secret/random" |
| 8 | + |
| 9 | + rdb "github.com/scaleway/scaleway-sdk-go/api/rdb/v1" |
| 10 | + secret "github.com/scaleway/scaleway-sdk-go/api/secret/v1beta1" |
| 11 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 12 | +) |
| 13 | + |
| 14 | +type rotateRequest struct { |
| 15 | + SecretID string `json:"secret_id"` |
| 16 | + RDBInstanceID string `json:"rdb_instance_id"` |
| 17 | +} |
| 18 | + |
| 19 | +type databaseCredentials struct { |
| 20 | + Engine string `json:"engine"` |
| 21 | + Username string `json:"username"` |
| 22 | + Password string `json:"password"` |
| 23 | + Hostname string `json:"host"` |
| 24 | + DBName string `json:"dbname"` |
| 25 | + Port string `json:"port"` |
| 26 | +} |
| 27 | + |
| 28 | +func Handle(w http.ResponseWriter, r *http.Request) { |
| 29 | + defer r.Body.Close() |
| 30 | + |
| 31 | + var req rotateRequest |
| 32 | + err := json.NewDecoder(r.Body).Decode(&req) |
| 33 | + if err != nil { |
| 34 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + client, err := scw.NewClient(scw.WithEnv()) |
| 39 | + if err != nil { |
| 40 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + rdbApi := rdb.NewAPI(client) |
| 45 | + secretApi := secret.NewAPI(client) |
| 46 | + |
| 47 | + // access current secret version to get revision and payload |
| 48 | + currentVersion, err := secretApi.AccessSecretVersion(&secret.AccessSecretVersionRequest{ |
| 49 | + SecretID: req.SecretID, |
| 50 | + Revision: "latest_enabled", |
| 51 | + }) |
| 52 | + if err != nil { |
| 53 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + // generate new password |
| 58 | + newPassword, err := random.CreateString(random.StringParams{ |
| 59 | + Length: 16, |
| 60 | + Upper: true, |
| 61 | + MinUpper: 1, |
| 62 | + Lower: true, |
| 63 | + MinLower: 1, |
| 64 | + Numeric: true, |
| 65 | + MinNumeric: 1, |
| 66 | + Special: true, |
| 67 | + MinSpecial: 1, |
| 68 | + }) |
| 69 | + |
| 70 | + if err != nil { |
| 71 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + // deserialize secret payload to access values |
| 76 | + var payload databaseCredentials |
| 77 | + err = json.Unmarshal(currentVersion.Data, &payload) |
| 78 | + if err != nil { |
| 79 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + // update RDB user with new password |
| 84 | + _, err = rdbApi.UpdateUser(&rdb.UpdateUserRequest{ |
| 85 | + InstanceID: req.RDBInstanceID, |
| 86 | + Password: scw.StringPtr(string(newPassword)), |
| 87 | + Name: payload.Username, |
| 88 | + }) |
| 89 | + if err != nil { |
| 90 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + payload.Password = string(newPassword) |
| 95 | + |
| 96 | + newData, err := json.Marshal(payload) |
| 97 | + if err != nil { |
| 98 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + // create new version of the secret with same content and password updated |
| 103 | + _, err = secretApi.CreateSecretVersion(&secret.CreateSecretVersionRequest{ |
| 104 | + SecretID: req.SecretID, |
| 105 | + Data: newData, |
| 106 | + DisablePrevious: scw.BoolPtr(true), |
| 107 | + }) |
| 108 | + if err != nil { |
| 109 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + fmt.Fprint(w, "database credentials updated") |
| 114 | +} |
0 commit comments