diff --git a/controllers/admiralty.go b/controllers/admiralty.go index db5335b..8e7ea2c 100644 --- a/controllers/admiralty.go +++ b/controllers/admiralty.go @@ -77,9 +77,10 @@ func (c *AdmiraltyController) GetAllTargets() { serv, err := infrastructure.NewService() if err != nil { // change code to 500 - c.Ctx.Output.SetStatus(500) - c.ServeJSON() - c.Data["json"] = map[string]string{"error": err.Error()} + HandleControllerErrors(c.Controller,500,&err,nil) + // c.Ctx.Output.SetStatus(500) + // c.ServeJSON() + // c.Data["json"] = map[string]string{"error": err.Error()} return } @@ -118,9 +119,7 @@ func (c *AdmiraltyController) GetOneTarget() { // @Title CreateSource // @Description Create an Admiralty Source on remote cluster -// @Param dc_id path string true "which dc to contact" // @Param execution path string true "execution id of the workflow" -// @Param kubeconfigInfo body controllers.KubeInfo true "url and serviceAccount to use with the source formatted as json object" // @Success 200 // @router /source/:id [post] func (c *AdmiraltyController) CreateSource() { @@ -140,15 +139,9 @@ func (c *AdmiraltyController) CreateSource() { fmt.Println("") fmt.Println("Key : ", data.KubeKey) - dc_id := c.Ctx.Input.Param(":dc_id") + execution := c.Ctx.Input.Param(":execution") - _ = dc_id - serv, err := infrastructure.NewRemoteKubernetesService( - *data.Url, - *data.KubeCA, - *data.KubeCert, - *data.KubeKey, - ) + serv, err := infrastructure.NewKubernetesService() if err != nil { // change code to 500 c.Ctx.Output.SetStatus(500) @@ -177,15 +170,14 @@ func (c *AdmiraltyController) CreateSource() { // @Title CreateAdmiraltyTarget // @Description Create an Admiralty Target in the namespace associated to the executionID -// @Param dc_id path string true "which dc to contact" // @Param execution path string true "execution id of the workflow" // @Success 201 // @router /target/:id [post] func (c *AdmiraltyController) CreateAdmiraltyTarget(){ var data map[string]interface{} - dc_id := c.Ctx.Input.Param(":dc_id") + execution := c.Ctx.Input.Param(":execution") - _ = dc_id + serv, err := infrastructure.NewService() if err != nil { @@ -229,15 +221,14 @@ func (c *AdmiraltyController) CreateAdmiraltyTarget(){ // @Title GetKubeSecret // @Description Retrieve the secret created from a Kubeconfig that will be associated to an Admiralty Target -// @Param dc_id path string true "which dc to contact" + // @Param execution path string true "execution id of the workflow" // @Success 200 // @router /secret/:id [get] func(c *AdmiraltyController) GetKubeSecret() { var data map[string]interface{} - dc_id := c.Ctx.Input.Param(":dc_id") + execution := c.Ctx.Input.Param(":execution") - _ = dc_id serv, err := infrastructure.NewService() if err != nil { @@ -278,7 +269,7 @@ func(c *AdmiraltyController) GetKubeSecret() { // @Title CreateKubeSecret // @Description Creat a secret from a Kubeconfig that will be associated to an Admiralty Target -// @Param dc_id path string true "which dc to contact" + // @Param execution path string true "execution id of the workflow" // @Param kubeconfig body controllers.Kubeconfig true "Kubeconfig to use when creating secret" // @Success 200 @@ -299,9 +290,9 @@ func (c *AdmiraltyController) CreateKubeSecret() { return } - dc_id := c.Ctx.Input.Param(":dc_id") + execution := c.Ctx.Input.Param(":execution") - _ = dc_id + serv, err := infrastructure.NewService() if err != nil { @@ -329,16 +320,16 @@ func (c *AdmiraltyController) CreateKubeSecret() { // @name GetAdmiraltyNodes // @description Allows user to test if an admiralty connection has already been established : Target and valid Secret set up on the local host and Source set up on remote host -// @Param dc_id path string true "which dc to contact" + // @Param execution path string true "execution id of the workflow" // @Success 200 // @Success 203 // @router /node/:id [get] func (c *AdmiraltyController) GetNodeReady(){ var secret v1.Secret - dc_id := c.Ctx.Input.Param(":dc_id") + execution := c.Ctx.Input.Param(":execution") - _ = dc_id + serv, err := infrastructure.NewService() if err != nil { @@ -384,7 +375,6 @@ func (c *AdmiraltyController) GetNodeReady(){ // Extract JWT token RS265 encoded var editedKubeconfig map[string]interface{} - var kubeUsers []KubeUser json.Unmarshal(resp,&secret) byteEditedKubeconfig := secret.Data["config"] err = yaml.Unmarshal(byteEditedKubeconfig,&editedKubeconfig) @@ -397,25 +387,26 @@ func (c *AdmiraltyController) GetNodeReady(){ c.ServeJSON() return } - b, err := json.Marshal(editedKubeconfig["users"]) - err = yaml.Unmarshal(b,&kubeUsers) - token := kubeUsers[0].User.Token + + token, err := retrieveTokenFromKonfig(editedKubeconfig) + if err != nil { + + } // Decode token - t, _, err := new(jwt.Parser).ParseUnverified(token, jwt.MapClaims{}) + isExpired, err := isTokenExpired(token) if err != nil { - fmt.Println("couldn't decode token") - c.Data["json"] = false + fmt.Println("Error veryfing token's expiration") + c.Ctx.Output.SetStatus(500) + c.Data["json"] = err c.ServeJSON() } - expiration, err := t.Claims.GetExpirationTime() - fmt.Println("Expiration date : " + expiration.UTC().Format("2006-01-02T15:04:05")) - - if expiration.Add(1 * time.Hour).Unix() < time.Now().Unix() { + if *isExpired { c.Data["json"] = map[string]string{ "token" : "token in the secret is expired and must be regenerated", } + c.Ctx.Output.SetStatus(410) c.ServeJSON() } @@ -424,16 +415,50 @@ func (c *AdmiraltyController) GetNodeReady(){ } +func retrieveTokenFromKonfig(editedKubeconfig map[string]interface{}) (string,error) { + var kubeUsers []KubeUser + b, err := json.Marshal(editedKubeconfig["users"]) + if err != nil { + fmt.Println() + } + err = yaml.Unmarshal(b,&kubeUsers) + if err != nil { + + } + token := kubeUsers[0].User.Token + + return token, nil +} + +func isTokenExpired(token string) (*bool, error){ + t, _, err := new(jwt.Parser).ParseUnverified(token, jwt.MapClaims{}) + if err != nil { + fmt.Println("couldn't decode token") + return nil, err + } + + expiration, err := t.Claims.GetExpirationTime() + if err != nil { + fmt.Println("Error while checking token's expiration time") + return nil, err + } + fmt.Println("Expiration date : " + expiration.UTC().Format("2006-01-02T15:04:05")) + + expired := expiration.Unix() < time.Now().Unix() + + return &expired, nil +} + // @name Get Admiralty Kubeconfig // @description Retrieve a kubeconfig from the host with the token to authenticate as the SA from the namespace identified with execution id -// @Param dc_id path string true "which dc to contact" + // @Param execution path string true "execution id of the workflow" // @Success 200 // @router /kubeconfig/:id [get] func (c *AdmiraltyController) GetAdmiraltyKubeconfig() { - dc_id := c.Ctx.Input.Param(":dc_id") + execution := c.Ctx.Input.Param(":execution") - _ = dc_id + serv, err := infrastructure.NewService() if err != nil { @@ -478,13 +503,12 @@ func (c *AdmiraltyController) GetAdmiraltyKubeconfig() { } c.ServeJSON() - return } func NewHostKubeWithToken(token string) (*models.KubeConfigValue, error){ if len(token) == 0 { - return nil, fmt.Errorf("You didn't provide a token to be inserted in the Kubeconfig") + return nil, fmt.Errorf("you didn't provide a token to be inserted in the Kubeconfig") } encodedCA := base64.StdEncoding.EncodeToString([]byte(conf.GetConfig().KubeCA))