diff --git a/controllers/minio.go b/controllers/minio.go index 61807e7..0592dee 100644 --- a/controllers/minio.go +++ b/controllers/minio.go @@ -16,7 +16,7 @@ type MinioController struct { // @Title CreateServiceAccounnt // @Description Add a new ServiceAccount to a Minio server using its ID and an execution ID and store the secret holding the login in the appropriate namespace -// @Success 200 +// @Success 201 // @Param executions path string true "The executionsID of the execution" // @Param minioId path string true "The ID of the Minio you want to reach" // @Param retrieve body map[string]string false "Should be empty or contain "'retrieve': true" @@ -153,4 +153,65 @@ func findLiveStorage(storageId string, peerId string) *live.LiveStorage { } return nil -} \ No newline at end of file +} + +// @Title CreateCredentialsSecret +// @Description Create a Kubernetes secret holding the access and secret keys to a given S3 server and bucket +// @Success 201 +// @Param executions path string true "The executionsID of the execution" +// @Param minioId path string true "The ID of the Minio youto which the credentials give access to" +// @Param creds body map[string]string true "The credentials to store in the secret" +// @router /secret/:minioId/:executions [post] +func (m *MinioController) CreateCredentialHoldingSecret(){ + executionsId := m.Ctx.Input.Param(":executions") + minioId := m.Ctx.Input.Param(":minioId") + + + var creds map[string]string + json.Unmarshal(m.Ctx.Input.CopyBody(10000), &creds) + + access, aOk := creds["access"] + secret, sOk := creds["secret"] + + if !aOk || !sOk || len(access) == 0 || len(secret) == 0 { + m.Ctx.Output.SetStatus(403) + m.Data["json"] = map[string]interface{}{"error": "Missing credentials"} + m.ServeJSON() + } + + // test if the namespace exists + k, err := infrastructure.NewService() + if err != nil { + m.Ctx.Output.SetStatus(500) + m.Data["json"] = map[string]string{"error": err.Error()} + m.ServeJSON() + return + } + + ns, err := k.GetNamespace(m.Ctx.Request.Context(), executionsId) + if ns == nil { + m.Ctx.Output.SetStatus(403) + m.Data["json"] = map[string]string{"error":"Could not find the namespace corresponding to executionsID " + executionsId} + m.ServeJSON() + return + } + if err != nil { + m.Ctx.Output.SetStatus(500) + m.Data["json"] = map[string]string{"error": "Error when trying to check if namespace " + executionsId + " exists : " + err.Error()} + m.ServeJSON() + return + } + + // store the credentials in the namespace + err = k.CreateSecret(m.Ctx.Request.Context(), minioId, executionsId, creds["access"], creds["secret"]) + if err != nil { + m.Ctx.Output.SetStatus(500) + m.Data["json"] = map[string]string{"error": "Error when storing Minio serviceAccount credentials in namespace " + executionsId + " exists : " + err.Error()} + m.ServeJSON() + return + } + + m.Ctx.Output.SetStatus(201) + + +}