console_conf: identity: use snapd unix socket

When running in strict snap confinement snap client binary is not
accessible. Additionally output format of snap client binary is
not guaranteed not to change. snapd REST API should be used instead.

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>
This commit is contained in:
Ondrej Kubik 2023-10-10 14:06:02 +01:00 committed by Maciej Borzecki
parent f03d5b5c5a
commit 55a1bff166
1 changed files with 7 additions and 6 deletions

View File

@ -13,7 +13,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import logging
import os
import pwd
@ -178,18 +177,20 @@ class IdentityController(TuiController):
login_details_path = self.opts.output_base + "/login-details.txt"
else:
self.app.urwid_loop.draw_screen()
cp = run_command(["snap", "create-user", "--sudoer", "--json", email])
if cp.returncode != 0:
con = SnapdConnection("", "/run/snapd.socket")
user_action = {"action": "create", "email": email, "sudoer": True}
res = con.post("v2/users", body=user_action)
if res.json()["status"] != "OK":
if isinstance(self.ui.body, IdentityView):
self.ui.body.snap_create_user_failed(
"Creating user failed:", cp.stderr
"Creating user failed:", res.json()["result"]["message"]
)
return
else:
data = json.loads(cp.stdout)
username = res.json()["result"][0]["username"]
result = {
"realname": email,
"username": data["username"],
"username": username,
}
login_details_path = self.app.state_path("login-details.txt")
self.model.add_user(result)