3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from am_api_base import *
16 from cmframework.apis import cmclient
19 class UsersKeys(AMApiBase):
22 User add key operations
24 .. :quickref: User keys;User add key operations
26 .. http:post:: /am/v1/users/keys
28 **Start User add key**
34 POST am/v1/users/keys HTTP/1.1
35 Host: haproxyvip:61200
36 Accept: application/json
38 "user": <uuid> or <username>
42 :> json string user: The user's id or name.
43 :> json string key: The user's public key.
52 "description": "User public key uploaded!"
55 :> json int code: the status code
56 :> json string description: the error description, present if code is non zero
58 User remove key operations
60 .. :quickref: User keys;User remove key operations
62 .. http:delete:: /am/v1/users/keys
64 **Start User remove key**
70 DELETE am/v1/users/keys HTTP/1.1
71 Host: haproxyvip:61200
72 Accept: application/json
74 "user": <uuid> or <username>
77 :> json string user: The user's id or name.
86 "description": "User public key removed!"
89 :> json int code: the status code
90 :> json string description: the error description, present if code is non zero
93 endpoints = ['users/keys']
94 parser_arguments = ['user',
98 self.logger.info("Received an add key request!")
99 args = self.parse_args()
101 if args["key"] is None:
102 self.logger.error("The public key is missing!")
103 return AMApiBase.embed_data({}, 1, "The public key is missing!")
105 state, user_info = self.get_uuid_and_name(args["user"])
107 state, message = self.user_checker(user_info, args["key"])
109 self.logger.info("User public key uploaded!")
110 return AMApiBase.embed_data({}, 0, "User public key uploaded!")
112 return AMApiBase.embed_data({}, 1, "Internal error: {0}".format(message))
114 self.logger.error(user_info)
115 return AMApiBase.embed_data({}, 1, user_info)
118 self.logger.info("Received a remove key request!")
119 args = self.parse_args()
121 state, user_info = self.get_uuid_and_name(args["user"])
123 state, message = self.user_checker(user_info, "")
125 self.logger.info("User public key removed!")
126 return AMApiBase.embed_data({}, 0, "User public key removed!")
128 return AMApiBase.embed_data({}, 1, "Internal error: {0}".format(message))
130 self.logger.error(user_info)
131 return AMApiBase.embed_data({}, 1, user_info)
133 def user_checker(self, user_info, key):
134 state_open, message_open = self._open_db()
137 roles = self.db.get_user_roles(user_info["id"])
138 self.logger.debug("Check the chroot role, when setting a user public key!")
140 self.logger.debug("Role name: {0}".format(role))
141 if self.db.is_chroot_role(role):
142 self.logger.debug("Found a chroot role attached to the {0} user!".format(user_info["name"]))
143 self.key_handler(user_info["name"], "Chroot", 'cloud.chroot', key)
145 if role == "linux_user":
146 self.logger.debug("Found a Linux user role attached to the {0} user!".format(user_info["name"]))
147 self.key_handler(user_info["name"], "Linux", 'cloud.linuxuser', key)
148 except Exception as ex:
149 self.logger.error("Internal error: {0}".format(ex))
152 state_close, message_close = self._close_db()
157 return False, message_open
159 def key_handler(self, username, user_type, list_name, key):
160 cmc = cmclient.CMClient()
161 user_list = cmc.get_property(list_name)
162 user_list = json.loads(user_list)
163 self.logger.debug("{0} user list before the change: {1}".format(user_type, json.dumps(user_list)))
165 self.logger.debug("The {0} user list exists!".format(user_type))
166 for val in user_list:
167 if val["name"] == username:
168 val["public_key"] = key
170 self.logger.debug("{0} user list after the change: {1}".format(user_type, json.dumps(user_list)))
171 cmc.set_property(list_name, json.dumps(user_list))