Exemplo n.º 1
0
func putUser(request *restful.Request, response *restful.Response) {
	name := request.PathParameter("name")

	user := rbac.User{}
	err := request.ReadEntity(&user)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["name"] = name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	if name != user.Name {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Path parameter name is different from name in the body"
		jsonMap["path"] = name
		jsonMap["body"] = user.Name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	oldUser, _ := authorization.GetStorage().LoadUser(name)
	if oldUser == nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "The user to update deosn't exist"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["name"] = name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(404, string(errorMessageByteSlice))
		return
	}

	createdUser := rbac.CreateUser(user.Name, user.EncodedPassword, user.RoleSlice, user.ResourceSlice, user.Description, user.MetaDataMap, user.ExpiredTime, user.Disabled)

	err = authorization.GetStorage().SaveUser(createdUser)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Save user failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["user"] = user
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}
}
Exemplo n.º 2
0
func putRole(request *restful.Request, response *restful.Response) {
	name := request.PathParameter("name")

	role := rbac.Role{}
	err := request.ReadEntity(&role)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["name"] = name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	if name != role.Name {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Path parameter name is different from name in the body"
		jsonMap["path"] = name
		jsonMap["body"] = role.Name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	oldRole, _ := authorization.GetStorage().LoadRole(role.Name)
	if oldRole == nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "The role to update deosn't exist"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["name"] = role.Name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(404, string(errorMessageByteSlice))
		return
	}

	err = authorization.GetStorage().SaveRole(&role)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Save role failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["role"] = role
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}
}
Exemplo n.º 3
0
func putUserMetaData(request *restful.Request, response *restful.Response) {
	name := request.PathParameter("name")

	metaDataMap := make(map[string]string)
	err := request.ReadEntity(&metaDataMap)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["name"] = name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	user, _ := authorization.GetStorage().LoadUser(name)
	if user == nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "The user to update deosn't exist"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["name"] = name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(404, string(errorMessageByteSlice))
		return
	}

	user.MetaDataMap = metaDataMap

	err = authorization.GetStorage().SaveUser(user)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Save user metadata failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["user"] = user
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}
}
Exemplo n.º 4
0
func postUser(request *restful.Request, response *restful.Response) {
	user := rbac.User{}
	err := request.ReadEntity(&user)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	oldUser, _ := authorization.GetStorage().LoadUser(user.Name)
	if oldUser != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "The user to create already exists"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["name"] = user.Name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(409, string(errorMessageByteSlice))
		return
	}

	createdUser := rbac.CreateUser(user.Name, user.EncodedPassword, user.RoleSlice, user.ResourceSlice, user.Description, user.MetaDataMap, user.ExpiredTime, user.Disabled)

	err = authorization.GetStorage().SaveUser(createdUser)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Save user failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["user"] = user
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}
}
Exemplo n.º 5
0
func getAllRole(request *restful.Request, response *restful.Response) {
	roleSlice, err := authorization.GetStorage().LoadAllRole()
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Get all role failure"
		jsonMap["ErrorMessage"] = err.Error()
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(404, string(errorMessageByteSlice))
		return
	}

	response.WriteJson(roleSlice, "[]Role")
}
Exemplo n.º 6
0
func postRole(request *restful.Request, response *restful.Response) {
	role := rbac.Role{}
	err := request.ReadEntity(&role)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	oldRole, _ := authorization.GetStorage().LoadRole(role.Name)
	if oldRole != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "The role to create already exists"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["name"] = role.Name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(409, string(errorMessageByteSlice))
		return
	}

	err = authorization.GetStorage().SaveRole(&role)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Save role failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["role"] = role
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}
}
Exemplo n.º 7
0
func deleteRole(request *restful.Request, response *restful.Response) {
	name := request.PathParameter("name")

	err := authorization.GetStorage().DeleteRole(name)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Delete role failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["name"] = name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(404, string(errorMessageByteSlice))
		return
	}
}
Exemplo n.º 8
0
func getUser(request *restful.Request, response *restful.Response) {
	name := request.PathParameter("name")

	user, err := authorization.GetStorage().LoadUser(name)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Get user failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["name"] = name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(404, string(errorMessageByteSlice))
		return
	}

	response.WriteJson(user, "User")
}
Exemplo n.º 9
0
func Notify(username string, imageInformationName string, signature string, payload string, kubeApiServerEndPoint string, kubeApiServerToken string) error {
	if len(username) == 0 {
		log.Error("User couldn't be empty. Signature %s", signature)
		log.Debug(payload)
		return errors.New("User couldn't be empty")
	}

	if len(signature) == 0 {
		return errors.New("The secret is required")
	}

	user, err := authorization.GetStorage().LoadUser(username)
	etcdError, _ := err.(client.Error)
	if etcdError.Code == client.ErrorCodeKeyNotFound {
		return errors.New("The user " + username + " doesn't exist")
	}
	if err != nil {
		log.Error("Get user error %s. User name %s, signature %s", err, username, signature)
		log.Debug(payload)
		return err
	}

	// If secret is used
	githubWebhookSecret := user.MetaDataMap["githubWebhookSecret"]
	generatedSignature := getGitHashSignature(githubWebhookSecret, payload)
	if generatedSignature != signature {
		log.Error("The signature is invalid. User name %s, signature %s, generated signature %s", username, signature, generatedSignature)
		log.Debug(payload)
		return errors.New("The signature is invalid")
	}

	jsonMap := make(map[string]interface{})
	err = json.Unmarshal([]byte(payload), &jsonMap)
	if err != nil {
		log.Error("Unmarshal payload error %s. User name %s, signature %s", err, username, signature)
		log.Debug(payload)
		return err
	}

	pusherJsonMap, _ := jsonMap["pusher"].(map[string]interface{})
	pusherName, _ := pusherJsonMap["name"].(string)

	repositoryJsonMap, _ := jsonMap["repository"].(map[string]interface{})
	cloneUrl, _ := repositoryJsonMap["clone_url"].(string)

	if len(cloneUrl) == 0 {
		log.Error("Can't find clone_url in github payload. User name %s, signature %s", username, signature)
		log.Debug(payload)
		return errors.New("Can't find clone_url in github payload")
	}

	imageInformation, err := image.GetStorage().LoadImageInformation(imageInformationName)
	etcdError, _ = err.(client.Error)
	if etcdError.Code == client.ErrorCodeKeyNotFound {
		return errors.New("The repository " + imageInformationName + " doesn't exist")
	}
	if err != nil {
		log.Error(err)
		return err
	}

	sourceCodeURL := imageInformation.BuildParameter["sourceCodeURL"]
	if sourceCodeURL != cloneUrl {
		// Not the target, ignore.
		return nil
	}

	if len(imageInformationName) == 0 {
		log.Error("Can't find image information using the github url %s. User name %s, signature %s", cloneUrl, username, signature)
		log.Debug(payload)
		return errors.New("Can't find image information using the github url")
	}

	// Asyncronized build
	go func() {
		outputMessage, err := image.BuildUpgrade(imageInformationName, "Github webhook. Pusher: "+pusherName)
		if err != nil {
			log.Error(err)
			log.Debug(outputMessage)
		} else {
			// Build sccessfully
			// Auto rolling update the deployment if configured
			imageInformation, err := image.GetStorage().LoadImageInformation(imageInformationName)
			if err != nil {
				log.Error(err)
			} else {
				deployInformationSlice, err := deploy.GetDeployInformationWithAutoUpdateForNewBuild(imageInformationName)
				if err != nil {
					log.Error(err)
				} else {
					for _, deployInformation := range deployInformationSlice {
						description := "Trigged by version " + imageInformation.CurrentVersion
						err := deploy.DeployUpdate(
							kubeApiServerEndPoint,
							kubeApiServerToken,
							deployInformation.Namespace,
							imageInformation.Name,
							imageInformation.CurrentVersion,
							description,
							deployInformation.EnvironmentSlice)
						if err != nil {
							log.Error(err)
						}
					}
				}
			}
		}
	}()

	return nil
}