From 81c67780fff68136c7821cf532d59fc1b527da90 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Wed, 5 Jan 2022 15:53:19 +0100 Subject: [PATCH] Add basics checks for token validity in the autoinstall JSON schema Signed-off-by: Olivier Gayot --- autoinstall-schema.json | 6 +++++- examples/autoinstall.yaml | 3 ++- scripts/runtests.sh | 2 +- subiquity/server/controllers/ubuntu_advantage.py | 8 ++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/autoinstall-schema.json b/autoinstall-schema.json index 48fb6778..0e89c79c 100644 --- a/autoinstall-schema.json +++ b/autoinstall-schema.json @@ -264,7 +264,11 @@ "type": "object", "properties": { "token": { - "type": "string" + "type": "string", + "minLength": 24, + "maxLength": 30, + "pattern": "^C[1-9A-HJ-NP-Za-km-z]+$", + "description": "A valid token starts with a C and is followed by 23 to 29 Base58 characters.\nSee https://pkg.go.dev/github.com/btcsuite/btcutil/base58#CheckEncode" } } }, diff --git a/examples/autoinstall.yaml b/examples/autoinstall.yaml index b3aaf5c5..4685f3b9 100644 --- a/examples/autoinstall.yaml +++ b/examples/autoinstall.yaml @@ -43,7 +43,8 @@ snaps: updates: all timezone: Pacific/Guam ubuntu-advantage: - token: "dummy-token" + # Token that passes the basic format checking but is invalid (i.e. contains more than 16 bytes of random data) + token: C1NWcZTHLteJXGVMM6YhvHDpGrhyy7 storage: config: - {type: disk, ptable: gpt, path: /dev/vdb, wipe: superblock, preserve: false, grub_device: true, id: disk-1} diff --git a/scripts/runtests.sh b/scripts/runtests.sh index ffe86f89..8a388a5f 100755 --- a/scripts/runtests.sh +++ b/scripts/runtests.sh @@ -161,7 +161,7 @@ python3 scripts/check-yaml-fields.py .subiquity/var/log/installer/subiquity-curt python3 scripts/check-yaml-fields.py <(python3 scripts/check-yaml-fields.py .subiquity/etc/cloud/cloud.cfg.d/99-installer.cfg datasource.None.userdata_raw) \ locale='"en_GB.UTF-8"' \ timezone='"Pacific/Guam"' \ - ubuntu_advantage.token='"dummy-token"' \ + ubuntu_advantage.token='"C1NWcZTHLteJXGVMM6YhvHDpGrhyy7"' \ 'snap.commands=[snap install --channel=3.2/stable etcd]' grep -q 'finish: subiquity/Install/install/postinstall/install_package1: SUCCESS: installing package1' \ .subiquity/subiquity-server-debug.log diff --git a/subiquity/server/controllers/ubuntu_advantage.py b/subiquity/server/controllers/ubuntu_advantage.py index c4f12374..c91cec47 100644 --- a/subiquity/server/controllers/ubuntu_advantage.py +++ b/subiquity/server/controllers/ubuntu_advantage.py @@ -22,6 +22,10 @@ from subiquity.server.controller import SubiquityController log = logging.getLogger("subiquity.server.controllers.ubuntu_advantage") +TOKEN_DESC = """\ +A valid token starts with a C and is followed by 23 to 29 Base58 characters. +See https://pkg.go.dev/github.com/btcsuite/btcutil/base58#CheckEncode""" + class UbuntuAdvantageController(SubiquityController): """ Represent the server-side Ubuntu Advantage controller. """ @@ -35,6 +39,10 @@ class UbuntuAdvantageController(SubiquityController): "properties": { "token": { "type": "string", + "minLength": 24, + "maxLength": 30, + "pattern": "^C[1-9A-HJ-NP-Za-km-z]+$", + "description": TOKEN_DESC, }, }, }