-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Api endpoint to provide users with the pro configuration information
- Loading branch information
1 parent
2312a49
commit ccc0264
Showing
7 changed files
with
285 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
Feature: Config status api | ||
|
||
Scenario Outline: u.pro.config.v1 | ||
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed | ||
When I run `pro api u.pro.config.v1` with sudo | ||
Then API data field output matches regexp: | ||
""" | ||
{ | ||
"attributes": { | ||
"apt_news": true, | ||
"apt_news_url": "https://motd.ubuntu.com/aptnews.json", | ||
"global_apt_http_proxy": null, | ||
"global_apt_https_proxy": null, | ||
"http_proxy": null, | ||
"https_proxy": null, | ||
"metering_timer": 14400, | ||
"ua_apt_http_proxy": null, | ||
"ua_apt_https_proxy": null, | ||
"update_messaging_timer": 21600 | ||
}, | ||
"meta": { | ||
"environment_vars": [] | ||
}, | ||
"type": "Config" | ||
} | ||
""" | ||
When I run `pro config set apt_news=false` with sudo | ||
When I run `pro api u.pro.config.v1` with sudo | ||
Then API data field output matches regexp: | ||
""" | ||
{ | ||
"attributes": { | ||
"apt_news": false, | ||
"apt_news_url": "https://motd.ubuntu.com/aptnews.json", | ||
"global_apt_http_proxy": null, | ||
"global_apt_https_proxy": null, | ||
"http_proxy": null, | ||
"https_proxy": null, | ||
"metering_timer": 14400, | ||
"ua_apt_http_proxy": null, | ||
"ua_apt_https_proxy": null, | ||
"update_messaging_timer": 21600 | ||
}, | ||
"meta": { | ||
"environment_vars": [] | ||
}, | ||
"type": "Config" | ||
} | ||
""" | ||
|
||
Examples: ubuntu release | ||
| release | machine_type | | ||
| xenial | lxd-container | | ||
| bionic | lxd-container | | ||
| focal | lxd-container | | ||
| jammy | lxd-container | | ||
| noble | lxd-container | | ||
|
||
Scenario Outline: Check proxy settings as sudo/non-root | ||
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed | ||
Given a `focal` `lxd-container` machine named `proxy` | ||
When I apt install `squid` on the `proxy` machine | ||
And I add this text on `/etc/squid/squid.conf` on `proxy` above `http_access deny all`: | ||
""" | ||
dns_v4_first on\nacl all src 0.0.0.0\/0\nhttp_access allow all | ||
""" | ||
And I run `systemctl restart squid.service` `with sudo` on the `proxy` machine | ||
And I run `pro config set http_proxy=http://someuser:somepassword@$behave_var{machine-ip proxy}:3128` with sudo | ||
When I run `pro api u.pro.config.v1` with sudo | ||
Then API data field output matches regexp: | ||
""" | ||
{ | ||
"attributes": { | ||
"apt_news": true, | ||
"apt_news_url": "https://motd.ubuntu.com/aptnews.json", | ||
"global_apt_http_proxy": null, | ||
"global_apt_https_proxy": null, | ||
"http_proxy": "http://someuser:somepassword@$behave_var{machine-ip proxy}:3128", | ||
"https_proxy": null, | ||
"metering_timer": 14400, | ||
"ua_apt_http_proxy": null, | ||
"ua_apt_https_proxy": null, | ||
"update_messaging_timer": 21600 | ||
}, | ||
"meta": { | ||
"environment_vars": [] | ||
}, | ||
"type": "Config" | ||
} | ||
""" | ||
When I run `pro api u.pro.config.v1` as non-root | ||
Then API data field output matches regexp: | ||
""" | ||
{ | ||
"attributes": { | ||
"apt_news": true, | ||
"apt_news_url": "https://motd.ubuntu.com/aptnews.json", | ||
"global_apt_http_proxy": null, | ||
"global_apt_https_proxy": null, | ||
"http_proxy": "<REDACTED>", | ||
"https_proxy": null, | ||
"metering_timer": 14400, | ||
"ua_apt_http_proxy": null, | ||
"ua_apt_https_proxy": null, | ||
"update_messaging_timer": 21600 | ||
}, | ||
"meta": { | ||
"environment_vars": [] | ||
}, | ||
"type": "Config" | ||
} | ||
""" | ||
|
||
Examples: ubuntu release | ||
| release | machine_type | | ||
| xenial | lxd-container | | ||
| bionic | lxd-container | | ||
| focal | lxd-container | | ||
| jammy | lxd-container | | ||
| noble | lxd-container | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import logging | ||
from typing import Optional | ||
|
||
from uaclient import util | ||
from uaclient.api.api import APIEndpoint | ||
from uaclient.api.data_types import AdditionalInfo | ||
from uaclient.config import UA_CONFIGURABLE_KEYS, UAConfig | ||
from uaclient.data_types import ( | ||
BoolDataValue, | ||
DataObject, | ||
Field, | ||
IntDataValue, | ||
StringDataValue, | ||
) | ||
|
||
LOG = logging.getLogger(util.replace_top_level_logger_name(__name__)) | ||
|
||
|
||
class ConfigInfo(DataObject, AdditionalInfo): | ||
fields = [ | ||
Field("http_proxy", StringDataValue, required=False, doc="HTTP proxy"), | ||
Field( | ||
"https_proxy", StringDataValue, required=False, doc="HTTPS proxy" | ||
), | ||
Field( | ||
"ua_apt_http_proxy", | ||
StringDataValue, | ||
required=False, | ||
doc="Ubuntu Pro APT HTTP proxy", | ||
), | ||
Field( | ||
"ua_apt_https_proxy", | ||
StringDataValue, | ||
required=False, | ||
doc="Ubuntu Pro APT HTTPS proxy", | ||
), | ||
Field( | ||
"global_apt_http_proxy", | ||
StringDataValue, | ||
required=False, | ||
doc="Global APT HTTP proxy", | ||
), | ||
Field( | ||
"global_apt_https_proxy", | ||
StringDataValue, | ||
required=False, | ||
doc="Global APT HTTPS proxy", | ||
), | ||
Field("apt_news", BoolDataValue, required=False, doc="APT news"), | ||
Field( | ||
"apt_news_url", StringDataValue, required=False, doc="APT news URL" | ||
), | ||
Field( | ||
"metering_timer", | ||
IntDataValue, | ||
required=False, | ||
doc="Metering timer", | ||
), | ||
Field( | ||
"update_messaging_timer", | ||
IntDataValue, | ||
required=False, | ||
doc="Update messaging timer", | ||
), | ||
] | ||
|
||
def __init__( | ||
self, | ||
*, | ||
http_proxy: Optional[str] = None, | ||
https_proxy: Optional[str] = None, | ||
ua_apt_http_proxy: Optional[str] = None, | ||
ua_apt_https_proxy: Optional[str] = None, | ||
global_apt_http_proxy: Optional[str] = None, | ||
global_apt_https_proxy: Optional[str] = None, | ||
update_messaging_timer: Optional[int] = None, | ||
metering_timer: Optional[int] = None, | ||
apt_news: Optional[bool] = None, | ||
apt_news_url: Optional[str] = None | ||
): | ||
self.http_proxy = http_proxy | ||
self.https_proxy = https_proxy | ||
self.ua_apt_http_proxy = ua_apt_http_proxy | ||
self.ua_apt_https_proxy = ua_apt_https_proxy | ||
self.global_apt_http_proxy = global_apt_http_proxy | ||
self.global_apt_https_proxy = global_apt_https_proxy | ||
self.update_messaging_timer = update_messaging_timer | ||
self.metering_timer = metering_timer | ||
self.apt_news = apt_news | ||
self.apt_news_url = apt_news_url | ||
|
||
|
||
def config() -> ConfigInfo: | ||
return _config(UAConfig()) | ||
|
||
|
||
def _config(cfg: UAConfig) -> ConfigInfo: | ||
"""This endpoint returns the current user configuration""" | ||
pro_config = {} | ||
for key in UA_CONFIGURABLE_KEYS: | ||
if hasattr(cfg, key): | ||
pro_config[key] = getattr(cfg, key) | ||
|
||
return ConfigInfo.from_dict(pro_config) | ||
|
||
|
||
endpoint = APIEndpoint( | ||
version="v1", | ||
name="Config", | ||
fn=_config, | ||
options_cls=None, | ||
) | ||
|
||
_doc = { | ||
"introduced_in": "35", | ||
"requires_network": False, | ||
"example_python": """ | ||
from uaclient.api.u.pro.config.v1 import config | ||
result = config() | ||
""", # noqa: E501 | ||
"result_class": ConfigInfo, | ||
"exceptions": [], | ||
"example_cli": "pro api u.pro.config.v1", | ||
"example_json": """ | ||
{ | ||
"attributes": { | ||
"apt_news": true, | ||
"apt_news_url": "https://motd.ubuntu.com/aptnews.json", | ||
"global_apt_http_proxy": null, | ||
"global_apt_https_proxy": null, | ||
"http_proxy": null, | ||
"https_proxy": null, | ||
"metering_timer": 14400, | ||
"ua_apt_http_proxy": null, | ||
"ua_apt_https_proxy": null, | ||
"update_messaging_timer": 21600 | ||
}, | ||
"meta": { | ||
"environment_vars": [] | ||
}, | ||
"type": "Config" | ||
} | ||
""", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters