diff --git a/products.d/tumbleweed.yaml b/products.d/tumbleweed.yaml index 6c5f0426a2..590008e631 100644 --- a/products.d/tumbleweed.yaml +++ b/products.d/tumbleweed.yaml @@ -154,10 +154,12 @@ storage: - mount_path: "swap" filesystem: swap size: - auto: false - min: 1 GiB - max: 2 GiB + auto: true outline: + auto_size: + base_min: 1 GiB + base_max: 2 GiB + adjust_by_ram: true required: false filesystems: - swap diff --git a/rust/Cargo.lock b/rust/Cargo.lock index cc576c6eaf..9d27a50a88 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -29,9 +29,11 @@ dependencies = [ "console", "convert_case", "fs_extra", + "home", "indicatif", "log", "nix 0.27.1", + "reqwest", "serde", "serde_json", "serde_yaml", @@ -41,49 +43,6 @@ dependencies = [ "zbus", ] -[[package]] -name = "agama-dbus-server" -version = "0.1.0" -dependencies = [ - "agama-lib", - "agama-locale-data", - "anyhow", - "async-trait", - "axum", - "axum-extra", - "chrono", - "cidr", - "clap", - "config", - "gettext-rs", - "http-body-util", - "jsonwebtoken", - "log", - "macaddr", - "once_cell", - "pam", - "rand", - "regex", - "serde", - "serde_json", - "serde_with", - "serde_yaml", - "simplelog", - "systemd-journal-logger", - "thiserror", - "tokio", - "tokio-stream", - "tower", - "tower-http", - "tracing", - "tracing-journald", - "tracing-subscriber", - "utoipa", - "uuid", - "zbus", - "zbus_macros", -] - [[package]] name = "agama-derive" version = "1.0.0" @@ -129,6 +88,49 @@ dependencies = [ "thiserror", ] +[[package]] +name = "agama-server" +version = "0.1.0" +dependencies = [ + "agama-lib", + "agama-locale-data", + "anyhow", + "async-trait", + "axum", + "axum-extra", + "chrono", + "cidr", + "clap", + "config", + "gettext-rs", + "http-body-util", + "jsonwebtoken", + "log", + "macaddr", + "once_cell", + "pam", + "rand", + "regex", + "serde", + "serde_json", + "serde_with", + "serde_yaml", + "simplelog", + "systemd-journal-logger", + "thiserror", + "tokio", + "tokio-stream", + "tower", + "tower-http", + "tracing", + "tracing-journald", + "tracing-subscriber", + "utoipa", + "uuid", + "zbus", + "zbus_macros", +] + [[package]] name = "agama-settings" version = "1.0.0" @@ -425,10 +427,10 @@ dependencies = [ "base64 0.21.7", "bytes", "futures-util", - "http", - "http-body", + "http 1.0.0", + "http-body 1.0.0", "http-body-util", - "hyper", + "hyper 1.1.0", "hyper-util", "itoa", "matchit", @@ -460,8 +462,8 @@ dependencies = [ "async-trait", "bytes", "futures-util", - "http", - "http-body", + "http 1.0.0", + "http-body 1.0.0", "http-body-util", "mime", "pin-project-lite", @@ -483,8 +485,8 @@ dependencies = [ "bytes", "futures-util", "headers", - "http", - "http-body", + "http 1.0.0", + "http-body 1.0.0", "http-body-util", "mime", "pin-project-lite", @@ -843,6 +845,16 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "core-foundation-sys" version = "0.8.6" @@ -1016,6 +1028,15 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" +[[package]] +name = "encoding_rs" +version = "0.8.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7268b386296a025e474d5140678f75d6de9493ae55a5d709eeb9dd08149945e1" +dependencies = [ + "cfg-if", +] + [[package]] name = "enumflags2" version = "0.7.8" @@ -1132,6 +1153,21 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -1296,6 +1332,25 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "h2" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb2c4422095b67ee78da96fbb51a4cc413b3b25883c7717ff7ca1ab31022c9c9" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 0.2.11", + "indexmap 2.1.0", + "slab", + "tokio", + "tokio-util", + "tracing", +] + [[package]] name = "h2" version = "0.4.2" @@ -1307,7 +1362,7 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http", + "http 1.0.0", "indexmap 2.1.0", "slab", "tokio", @@ -1342,7 +1397,7 @@ dependencies = [ "base64 0.21.7", "bytes", "headers-core", - "http", + "http 1.0.0", "httpdate", "mime", "sha1", @@ -1354,7 +1409,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "54b4a22553d4242c49fddb9ba998a99962b5cc6f22cb5a3482bec22522403ce4" dependencies = [ - "http", + "http 1.0.0", ] [[package]] @@ -1384,6 +1439,26 @@ dependencies = [ "digest", ] +[[package]] +name = "home" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "http" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8947b1a6fad4393052c7ba1f4cd97bed3e953a95c79c92ad9b051a04611d9fbb" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + [[package]] name = "http" version = "1.0.0" @@ -1395,6 +1470,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http 0.2.11", + "pin-project-lite", +] + [[package]] name = "http-body" version = "1.0.0" @@ -1402,7 +1488,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" dependencies = [ "bytes", - "http", + "http 1.0.0", ] [[package]] @@ -1413,8 +1499,8 @@ checksum = "41cb79eb393015dadd30fc252023adb0b2400a0caee0fa2a077e6e21a551e840" dependencies = [ "bytes", "futures-util", - "http", - "http-body", + "http 1.0.0", + "http-body 1.0.0", "pin-project-lite", ] @@ -1430,6 +1516,30 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "hyper" +version = "0.14.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2 0.3.24", + "http 0.2.11", + "http-body 0.4.6", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2 0.5.5", + "tokio", + "tower-service", + "tracing", + "want", +] + [[package]] name = "hyper" version = "1.1.0" @@ -1439,9 +1549,9 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "h2", - "http", - "http-body", + "h2 0.4.2", + "http 1.0.0", + "http-body 1.0.0", "httparse", "httpdate", "itoa", @@ -1449,6 +1559,19 @@ dependencies = [ "tokio", ] +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper 0.14.28", + "native-tls", + "tokio", + "tokio-native-tls", +] + [[package]] name = "hyper-util" version = "0.1.3" @@ -1457,9 +1580,9 @@ checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa" dependencies = [ "bytes", "futures-util", - "http", - "http-body", - "hyper", + "http 1.0.0", + "http-body 1.0.0", + "hyper 1.1.0", "pin-project-lite", "socket2 0.5.5", "tokio", @@ -1559,6 +1682,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "ipnet" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" + [[package]] name = "iso8601" version = "0.5.1" @@ -1820,6 +1949,24 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "native-tls" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07226173c32f2926027b63cce4bcd8076c3552846cbe7925f3aaffeac0a3b92e" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "nix" version = "0.26.4" @@ -2014,6 +2161,32 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "openssl" +version = "0.10.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f" +dependencies = [ + "bitflags 2.4.1", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.48", +] + [[package]] name = "openssl-probe" version = "0.1.5" @@ -2022,9 +2195,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-sys" -version = "0.9.97" +version = "0.9.101" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3eaad34cdd97d81de97964fc7f29e2d104f483840d906ef56daa1912338460b" +checksum = "dda2b0f344e78efc2facf7d195d098df0dd72151b26ab98da807afc26c198dff" dependencies = [ "cc", "libc", @@ -2462,6 +2635,46 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +[[package]] +name = "reqwest" +version = "0.11.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6920094eb85afde5e4a138be3f2de8bbdf28000f0029e72c45025a56b042251" +dependencies = [ + "base64 0.21.7", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2 0.3.24", + "http 0.2.11", + "http-body 0.4.6", + "hyper 0.14.28", + "hyper-tls", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls-pemfile", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "system-configuration", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + [[package]] name = "ring" version = "0.17.7" @@ -2537,6 +2750,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustls-pemfile" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" +dependencies = [ + "base64 0.21.7", +] + [[package]] name = "rustversion" version = "1.0.14" @@ -2564,6 +2786,29 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "security-framework" +version = "2.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e932934257d3b408ed8f30db49d85ea163bfe74961f017f405b025af298f0c7a" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "serde" version = "1.0.196" @@ -2848,6 +3093,27 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "system-configuration" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "systemd-journal-logger" version = "1.0.0" @@ -3011,6 +3277,16 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + [[package]] name = "tokio-stream" version = "0.1.14" @@ -3119,8 +3395,8 @@ dependencies = [ "bitflags 2.4.1", "bytes", "futures-core", - "http", - "http-body", + "http 1.0.0", + "http-body 1.0.0", "http-body-util", "pin-project-lite", "tokio", @@ -3211,6 +3487,12 @@ dependencies = [ "tracing-log", ] +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + [[package]] name = "tungstenite" version = "0.21.0" @@ -3220,7 +3502,7 @@ dependencies = [ "byteorder", "bytes", "data-encoding", - "http", + "http 1.0.0", "httparse", "log", "rand", @@ -3396,6 +3678,15 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3c4517f54858c779bbcbf228f4fca63d121bf85fbecb2dc578cdf4a39395690" +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -3427,6 +3718,18 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877b9c3f61ceea0e56331985743b13f3d25c406a7098d45180fb5f09bc19ed97" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.91" @@ -3456,6 +3759,16 @@ version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" +[[package]] +name = "web-sys" +version = "0.3.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96565907687f7aceb35bc5fc03770a8a0471d82e479f25832f54a0e3f4b28446" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "winapi" version = "0.3.9" @@ -3712,6 +4025,16 @@ dependencies = [ "memchr", ] +[[package]] +name = "winreg" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" +dependencies = [ + "cfg-if", + "windows-sys 0.48.0", +] + [[package]] name = "xdg-home" version = "1.0.0" diff --git a/rust/agama-cli/Cargo.toml b/rust/agama-cli/Cargo.toml index 51873d5e41..8a073f259e 100644 --- a/rust/agama-cli/Cargo.toml +++ b/rust/agama-cli/Cargo.toml @@ -25,6 +25,8 @@ nix = { version = "0.27.1", features = ["user"] } zbus = { version = "3", default-features = false, features = ["tokio"] } tokio = { version = "1.33.0", features = ["macros", "rt-multi-thread"] } async-trait = "0.1.77" +reqwest = { version = "0.11", features = ["json"] } +home = "0.5.9" [[bin]] name = "agama" diff --git a/rust/agama-cli/src/auth.rs b/rust/agama-cli/src/auth.rs new file mode 100644 index 0000000000..0e1c34bf13 --- /dev/null +++ b/rust/agama-cli/src/auth.rs @@ -0,0 +1,243 @@ +use clap::{arg, Args, Subcommand}; +use home; +use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE}; +use std::fs; +use std::fs::File; +use std::io; +use std::io::{BufRead, BufReader}; +use std::os::unix::fs::PermissionsExt; +use std::path::{Path, PathBuf}; + +const DEFAULT_JWT_FILE: &str = ".agama/agama-jwt"; +const DEFAULT_AUTH_URL: &str = "http://localhost:3000/api/authenticate"; +const DEFAULT_FILE_MODE: u32 = 0o600; + +#[derive(Subcommand, Debug)] +pub enum AuthCommands { + /// Login with defined server. Result is JWT stored locally and made available to + /// further use. Password can be provided by commandline option, from a file or it fallbacks + /// into an interactive prompt. + Login(LoginArgs), + /// Release currently stored JWT + Logout, + /// Prints currently stored JWT to stdout + Show, +} + +/// Main entry point called from agama CLI main loop +pub async fn run(subcommand: AuthCommands) -> anyhow::Result<()> { + match subcommand { + AuthCommands::Login(options) => login(LoginArgs::proceed(options).password()?).await, + AuthCommands::Logout => logout(), + AuthCommands::Show => show(), + } +} + +/// Reads stored token and returns it +fn jwt() -> anyhow::Result { + if let Some(file) = jwt_file() { + if let Ok(token) = read_line_from_file(&file.as_path()) { + return Ok(token); + } + } + + Err(anyhow::anyhow!("Authentication token not available")) +} + +/// Stores user provided configuration for login command +#[derive(Args, Debug)] +pub struct LoginArgs { + #[arg(long, short = 'p')] + password: Option, + #[arg(long, short = 'f')] + file: Option, +} + +impl LoginArgs { + /// Transforms user provided options into internal representation + /// See Credentials trait + fn proceed(options: LoginArgs) -> Box { + if let Some(password) = options.password { + Box::new(KnownCredentials { password }) + } else if let Some(path) = options.file { + Box::new(FileCredentials { path }) + } else { + Box::new(MissingCredentials {}) + } + } +} + +/// Placeholder for no configuration provided by user +struct MissingCredentials; + +/// Stores whatever is needed for reading credentials from a file +struct FileCredentials { + path: PathBuf, +} + +/// Stores credentials as provided by the user directly +struct KnownCredentials { + password: String, +} + +/// Transforms credentials from user's input into format used internaly +trait Credentials { + fn password(&self) -> io::Result; +} + +impl Credentials for KnownCredentials { + fn password(&self) -> io::Result { + Ok(self.password.clone()) + } +} + +impl Credentials for FileCredentials { + fn password(&self) -> io::Result { + read_line_from_file(&self.path.as_path()) + } +} + +impl Credentials for MissingCredentials { + fn password(&self) -> io::Result { + let password = read_credential("Password".to_string())?; + + Ok(password) + } +} + +/// Path to file where JWT is stored +fn jwt_file() -> Option { + Some(home::home_dir()?.join(DEFAULT_JWT_FILE)) +} + +/// Reads first line from given file +fn read_line_from_file(path: &Path) -> io::Result { + if !path.exists() { + return Err(io::Error::new( + io::ErrorKind::Other, + "Cannot find the file containing the credentials.", + )); + } + + if let Ok(file) = File::open(&path) { + // cares only of first line, take everything. No comments + // or something like that supported + let raw = BufReader::new(file).lines().next(); + + if let Some(line) = raw { + return line; + } + } + + Err(io::Error::new( + io::ErrorKind::Other, + "Failed to open the file", + )) +} + +/// Asks user to provide a line of input. Displays a prompt. +fn read_credential(caption: String) -> io::Result { + let mut cred = String::new(); + + println!("{}: ", caption); + + io::stdin().read_line(&mut cred)?; + if cred.pop().is_none() || cred.is_empty() { + return Err(io::Error::new( + io::ErrorKind::Other, + format!("Failed to read {}", caption), + )); + } + + Ok(cred) +} + +/// Sets the archive owner to root:root. Also sets the file permissions to read/write for the +/// owner only. +fn set_file_permissions(file: &Path) -> io::Result<()> { + let attr = fs::metadata(file)?; + let mut permissions = attr.permissions(); + + // set the file file permissions to -rw------- + permissions.set_mode(DEFAULT_FILE_MODE); + fs::set_permissions(file, permissions)?; + + Ok(()) +} + +/// Necessary http request header for authenticate +fn authenticate_headers() -> HeaderMap { + let mut headers = HeaderMap::new(); + + headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json")); + + headers +} + +/// Query web server for JWT +async fn get_jwt(url: String, password: String) -> anyhow::Result { + let client = reqwest::Client::new(); + let response = client + .post(url) + .headers(authenticate_headers()) + .body(format!("{{\"password\": \"{}\"}}", password)) + .send() + .await?; + let body = response + .json::>() + .await?; + let value = body.get(&"token".to_string()); + + if let Some(token) = value { + return Ok(token.clone()); + } + + Err(anyhow::anyhow!("Failed to get authentication token")) +} + +/// Logs into the installation web server and stores JWT for later use. +async fn login(password: String) -> anyhow::Result<()> { + // 1) ask web server for JWT + let res = get_jwt(DEFAULT_AUTH_URL.to_string(), password).await?; + + // 2) if successful store the JWT for later use + if let Some(path) = jwt_file() { + if let Some(dir) = path.parent() { + fs::create_dir_all(dir)?; + } else { + return Err(anyhow::anyhow!("Cannot store the authentication token")); + } + + fs::write(path.as_path(), res)?; + set_file_permissions(path.as_path())?; + } + + Ok(()) +} + +/// Releases JWT +fn logout() -> anyhow::Result<()> { + let path = jwt_file(); + + if !&path.clone().is_some_and(|p| p.exists()) { + // mask if the file with the JWT doesn't exist (most probably no login before logout) + return Ok(()); + } + + // panicking is right thing to do if expect fails, becase it was already checked twice that + // the path exists + let file = path.expect("Cannot locate stored JWT"); + + Ok(fs::remove_file(file)?) +} + +/// Shows stored JWT on stdout +fn show() -> anyhow::Result<()> { + // we do not care if jwt() fails or not. If there is something to print, show it otherwise + // stay silent + if let Ok(token) = jwt() { + println!("{}", token); + } + + Ok(()) +} diff --git a/rust/agama-cli/src/commands.rs b/rust/agama-cli/src/commands.rs index 99d51ece9f..46615a4abe 100644 --- a/rust/agama-cli/src/commands.rs +++ b/rust/agama-cli/src/commands.rs @@ -1,3 +1,4 @@ +use crate::auth::AuthCommands; use crate::config::ConfigCommands; use crate::logs::LogsCommands; use crate::profile::ProfileCommands; @@ -35,4 +36,7 @@ pub enum Commands { /// Collects logs #[command(subcommand)] Logs(LogsCommands), + /// Request an action on the web server like Login / Logout + #[command(subcommand)] + Auth(AuthCommands), } diff --git a/rust/agama-cli/src/main.rs b/rust/agama-cli/src/main.rs index 52a3fa4a89..33117ae481 100644 --- a/rust/agama-cli/src/main.rs +++ b/rust/agama-cli/src/main.rs @@ -1,5 +1,6 @@ use clap::Parser; +mod auth; mod commands; mod config; mod error; @@ -13,6 +14,7 @@ use crate::error::CliError; use agama_lib::error::ServiceError; use agama_lib::manager::ManagerClient; use agama_lib::progress::ProgressMonitor; +use auth::run as run_auth_cmd; use commands::Commands; use config::run as run_config_cmd; use logs::run as run_logs_cmd; @@ -135,6 +137,7 @@ async fn run_command(cli: Cli) -> anyhow::Result<()> { } Commands::Questions(subcommand) => run_questions_cmd(subcommand).await, Commands::Logs(subcommand) => run_logs_cmd(subcommand).await, + Commands::Auth(subcommand) => run_auth_cmd(subcommand).await, _ => unimplemented!(), } } diff --git a/rust/agama-server/Cargo.toml b/rust/agama-server/Cargo.toml index 25cc8ccba5..def882c4a2 100644 --- a/rust/agama-server/Cargo.toml +++ b/rust/agama-server/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "agama-dbus-server" +name = "agama-server" version = "0.1.0" edition = "2021" rust-version.workspace = true diff --git a/rust/agama-server/src/agama-dbus-server.rs b/rust/agama-server/src/agama-dbus-server.rs index 3145228fd3..7643d6e0ec 100644 --- a/rust/agama-server/src/agama-dbus-server.rs +++ b/rust/agama-server/src/agama-dbus-server.rs @@ -1,4 +1,4 @@ -use agama_dbus_server::{ +use agama_server::{ l10n::{self, helpers}, network, questions, }; diff --git a/rust/agama-server/src/agama-web-server.rs b/rust/agama-server/src/agama-web-server.rs index af96034b34..53f1e217c1 100644 --- a/rust/agama-server/src/agama-web-server.rs +++ b/rust/agama-server/src/agama-web-server.rs @@ -1,10 +1,10 @@ use std::process::{ExitCode, Termination}; -use agama_dbus_server::{ +use agama_lib::connection_to; +use agama_server::{ l10n::helpers, web::{self, run_monitor}, }; -use agama_lib::connection_to; use clap::{Args, Parser, Subcommand}; use tokio::sync::broadcast::channel; use tracing_subscriber::prelude::*; diff --git a/rust/agama-server/tests/l10n.rs b/rust/agama-server/tests/l10n.rs index f2d0291c29..75d0e76109 100644 --- a/rust/agama-server/tests/l10n.rs +++ b/rust/agama-server/tests/l10n.rs @@ -1,6 +1,6 @@ pub mod common; -use agama_dbus_server::l10n::web::l10n_service; +use agama_server::l10n::web::l10n_service; use axum::{ body::Body, http::{Request, StatusCode}, diff --git a/rust/agama-server/tests/network.rs b/rust/agama-server/tests/network.rs index ed4b0037b2..e437331d9d 100644 --- a/rust/agama-server/tests/network.rs +++ b/rust/agama-server/tests/network.rs @@ -1,16 +1,16 @@ pub mod common; use self::common::{async_retry, DBusServer}; -use agama_dbus_server::network::{ - self, - model::{self, Ipv4Method, Ipv6Method}, - Adapter, NetworkAdapterError, NetworkService, NetworkState, -}; use agama_lib::network::{ settings::{self}, types::DeviceType, NetworkClient, }; +use agama_server::network::{ + self, + model::{self, Ipv4Method, Ipv6Method}, + Adapter, NetworkAdapterError, NetworkService, NetworkState, +}; use async_trait::async_trait; use cidr::IpInet; use std::error::Error; diff --git a/rust/agama-server/tests/service.rs b/rust/agama-server/tests/service.rs index 14c5c4ae0d..daa453af02 100644 --- a/rust/agama-server/tests/service.rs +++ b/rust/agama-server/tests/service.rs @@ -1,6 +1,6 @@ pub mod common; -use agama_dbus_server::{ +use agama_server::{ service, web::{generate_token, MainServiceBuilder, ServiceConfig}, }; diff --git a/rust/package/agama.changes b/rust/package/agama.changes index 656cd074eb..8e64449ffe 100644 --- a/rust/package/agama.changes +++ b/rust/package/agama.changes @@ -1,3 +1,10 @@ +------------------------------------------------------------------- +Thu Mar 7 10:52:58 UTC 2024 - Michal Filka + +- CLI: added auth command with login / logout / show subcommands + for handling authentication token management with new agama web + server + ------------------------------------------------------------------- Tue Feb 27 15:55:28 UTC 2024 - Imobach Gonzalez Sosa diff --git a/service/lib/agama/dbus/storage/volume_conversion/to_dbus.rb b/service/lib/agama/dbus/storage/volume_conversion/to_dbus.rb index dd03df6dd8..3f586f7d2e 100644 --- a/service/lib/agama/dbus/storage/volume_conversion/to_dbus.rb +++ b/service/lib/agama/dbus/storage/volume_conversion/to_dbus.rb @@ -70,6 +70,7 @@ def outline_conversion(target) "Required" => outline.required?, "FsTypes" => outline.filesystems.map(&:to_human_string), "SupportAutoSize" => outline.adaptive_sizes?, + "AdjustByRam" => outline.adjust_by_ram?, "SnapshotsConfigurable" => outline.snapshots_configurable?, "SnapshotsAffectSizes" => outline.snapshots_affect_sizes?, "SizeRelevantVolumes" => outline.size_relevant_volumes diff --git a/service/test/agama/dbus/storage/proposal_settings_conversion/to_dbus_test.rb b/service/test/agama/dbus/storage/proposal_settings_conversion/to_dbus_test.rb index 8cc085e5ff..44ef43b0ca 100644 --- a/service/test/agama/dbus/storage/proposal_settings_conversion/to_dbus_test.rb +++ b/service/test/agama/dbus/storage/proposal_settings_conversion/to_dbus_test.rb @@ -92,6 +92,7 @@ "SupportAutoSize" => false, "SnapshotsConfigurable" => false, "SnapshotsAffectSizes" => false, + "AdjustByRam" => false, "SizeRelevantVolumes" => [] } } diff --git a/service/test/agama/dbus/storage/volume_conversion/to_dbus_test.rb b/service/test/agama/dbus/storage/volume_conversion/to_dbus_test.rb index 5b412ce508..9fffa181a2 100644 --- a/service/test/agama/dbus/storage/volume_conversion/to_dbus_test.rb +++ b/service/test/agama/dbus/storage/volume_conversion/to_dbus_test.rb @@ -38,6 +38,7 @@ outline.snapshots_configurable = true outline.snapshots_size = Y2Storage::DiskSize.new(1000) outline.snapshots_percentage = 10 + outline.adjust_by_ram = true end Agama::Storage::Volume.new("/test").tap do |volume| @@ -70,6 +71,7 @@ "Required" => false, "FsTypes" => [], "SupportAutoSize" => false, + "AdjustByRam" => false, "SnapshotsConfigurable" => false, "SnapshotsAffectSizes" => false, "SizeRelevantVolumes" => [] @@ -90,6 +92,7 @@ "Outline" => { "Required" => true, "FsTypes" => ["Ext3", "Ext4"], + "AdjustByRam" => true, "SupportAutoSize" => true, "SnapshotsConfigurable" => true, "SnapshotsAffectSizes" => true, diff --git a/web/src/assets/styles/blocks.scss b/web/src/assets/styles/blocks.scss index 30e95e4aa4..e568ae24db 100644 --- a/web/src/assets/styles/blocks.scss +++ b/web/src/assets/styles/blocks.scss @@ -469,6 +469,27 @@ ul[data-type="agama/list"][role="grid"] { } } +[data-type="agama/reminder"] { + --accent-color: var(--color-primary-lighter); + --inline-margin: calc(var(--header-icon-size) + var(--spacer-small)); + + display: flex; + gap: var(--spacer-small); + margin-inline: var(--inline-margin); + margin-block-end: var(--spacer-normal); + padding: var(--spacer-smaller) var(--spacer-small); + border-inline-start: 3px solid var(--accent-color); + + svg { + fill: var(--accent-color); + } + + h4 { + color: var(--accent-color); + margin-block-end: var(--spacer-smaller); + } +} + [role="dialog"] { section:not([class^="pf-c"]) { > svg:first-child { diff --git a/web/src/client/storage.js b/web/src/client/storage.js index d10857431f..b7d6135f4b 100644 --- a/web/src/client/storage.js +++ b/web/src/client/storage.js @@ -359,6 +359,7 @@ class ProposalManager { * @property {boolean} required * @property {string[]} fsTypes * @property {boolean} supportAutoSize + * @property {boolean} adjustByRam * @property {boolean} snapshotsConfigurable * @property {boolean} snapshotsAffectSizes * @property {string[]} sizeRelevantVolumes @@ -603,6 +604,7 @@ class ProposalManager { required: dbusOutline.Required.v, fsTypes: dbusOutline.FsTypes.v.map(val => val.v), supportAutoSize: dbusOutline.SupportAutoSize.v, + adjustByRam: dbusOutline.AdjustByRam.v, snapshotsConfigurable: dbusOutline.SnapshotsConfigurable.v, snapshotsAffectSizes: dbusOutline.SnapshotsAffectSizes.v, sizeRelevantVolumes: dbusOutline.SizeRelevantVolumes.v.map(val => val.v) diff --git a/web/src/client/storage.test.js b/web/src/client/storage.test.js index 5f68771c79..d78dc64263 100644 --- a/web/src/client/storage.test.js +++ b/web/src/client/storage.test.js @@ -478,6 +478,7 @@ const contexts = { SupportAutoSize: { t: "b", v: true }, SnapshotsConfigurable: { t: "b", v: true }, SnapshotsAffectSizes: { t: "b", v: true }, + AdjustByRam: { t: "b", v: false }, SizeRelevantVolumes: { t: "as", v: [{ t: "s", v: "/home" }] } } } @@ -498,6 +499,7 @@ const contexts = { SupportAutoSize: { t: "b", v: false }, SnapshotsConfigurable: { t: "b", v: false }, SnapshotsAffectSizes: { t: "b", v: false }, + AdjustByRam: { t: "b", v: false }, SizeRelevantVolumes: { t: "as", v: [] } } } @@ -1390,6 +1392,7 @@ describe("#proposal", () => { SupportAutoSize: { t: "b", v: false }, SnapshotsConfigurable: { t: "b", v: false }, SnapshotsAffectSizes: { t: "b", v: false }, + AdjustByRam: { t: "b", v: false }, SizeRelevantVolumes: { t: "as", v: [] } } } @@ -1410,6 +1413,7 @@ describe("#proposal", () => { SupportAutoSize: { t: "b", v: false }, SnapshotsConfigurable: { t: "b", v: false }, SnapshotsAffectSizes: { t: "b", v: false }, + AdjustByRam: { t: "b", v: false }, SizeRelevantVolumes: { t: "as", v: [] } } } @@ -1437,6 +1441,7 @@ describe("#proposal", () => { supportAutoSize: false, snapshotsConfigurable: false, snapshotsAffectSizes: false, + adjustByRam: false, sizeRelevantVolumes: [] } }); @@ -1457,6 +1462,7 @@ describe("#proposal", () => { supportAutoSize: false, snapshotsConfigurable: false, snapshotsAffectSizes: false, + adjustByRam: false, sizeRelevantVolumes: [] } }); diff --git a/web/src/components/core/Reminder.jsx b/web/src/components/core/Reminder.jsx new file mode 100644 index 0000000000..997e870a91 --- /dev/null +++ b/web/src/components/core/Reminder.jsx @@ -0,0 +1,84 @@ +/* + * Copyright (c) [2024] SUSE LLC + * + * All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, contact SUSE LLC. + * + * To contact SUSE LLC about this file by physical or electronic mail, you may + * find current contact information at www.suse.com. + */ + +// @ts-check + +import React from "react"; +import { Icon } from "~/components/layout"; + +/** + * Internal component for rendering the icon + * + * @param {object} props + * @params {string} [props.name] - The icon name. + */ +const ReminderIcon = ({ name }) => { + if (!name?.length) return; + + return ( +
+ +
+ ); +}; + +/** + * Internal component for rendering the title + * + * @param {object} props + * @params {JSX.Element|string} [props.children] - The title content. + */ +const ReminderTitle = ({ children }) => { + if (!children) return; + if (typeof children === "string" && !children.length) return; + + return ( +

{children}

+ ); +}; + +/** + * Renders a reminder with given role, status by default + * @component + * + * @param {object} props + * @param {string} [props.icon] - The name of desired icon. + * @param {JSX.Element|string} [props.title] - The content for the title. + * @param {string} [props.role="status"] - The reminder's role, "status" by + * default. See {@link https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/status_role} + * @param {JSX.Element} [props.children] - The content for the description. + */ +export default function Reminder ({ + icon, + title, + role = "status", + children +}) { + return ( +
+ +
+ {title} + { children } +
+
+ ); +} diff --git a/web/src/components/core/Reminder.test.jsx b/web/src/components/core/Reminder.test.jsx new file mode 100644 index 0000000000..24527cf2d0 --- /dev/null +++ b/web/src/components/core/Reminder.test.jsx @@ -0,0 +1,65 @@ +/* + * Copyright (c) [2023] SUSE LLC + * + * All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, contact SUSE LLC. + * + * To contact SUSE LLC about this file by physical or electronic mail, you may + * find current contact information at www.suse.com. + */ + +import React from "react"; +import { screen, within } from "@testing-library/react"; +import { plainRender } from "~/test-utils"; +import { Reminder } from "~/components/core"; + +describe("Reminder", () => { + it("renders a status region by default", () => { + plainRender(Example); + const reminder = screen.getByRole("status"); + within(reminder).getByText("Example"); + }); + + it("renders a region with given role", () => { + plainRender(Example); + const reminder = screen.getByRole("alert"); + within(reminder).getByText("Example"); + }); + + it("renders given title", () => { + plainRender( + Kindly reminder}> + Visit the settings section + + ); + screen.getByRole("heading", { name: "Kindly reminder", level: 4 }); + }); + + it("does not render a heading if title is not given", () => { + plainRender(Without title); + expect(screen.queryByRole("heading")).toBeNull(); + }); + + it("does not render a heading if title is an empty string", () => { + plainRender(Without title); + expect(screen.queryByRole("heading")).toBeNull(); + }); + + it("renders given children", () => { + plainRender( + Visit the settings section + ); + screen.getByRole("link", { name: "Visit the settings section" }); + }); +}); diff --git a/web/src/components/core/index.js b/web/src/components/core/index.js index 0213c66215..6bdac86248 100644 --- a/web/src/components/core/index.js +++ b/web/src/components/core/index.js @@ -57,3 +57,4 @@ export { default as PasswordInput } from "./PasswordInput"; export { default as DevelopmentInfo } from "./DevelopmentInfo"; export { default as Selector } from "./Selector"; export { default as OptionsPicker } from "./OptionsPicker"; +export { default as Reminder } from "./Reminder"; diff --git a/web/src/components/storage/ProposalPage.jsx b/web/src/components/storage/ProposalPage.jsx index ccb3bfdf4c..85e29c5b41 100644 --- a/web/src/components/storage/ProposalPage.jsx +++ b/web/src/components/storage/ProposalPage.jsx @@ -31,7 +31,8 @@ import { ProposalSettingsSection, ProposalSpacePolicySection, ProposalDeviceSection, - ProposalFileSystemsSection + ProposalFileSystemsSection, + ProposalTransactionalInfo } from "~/components/storage"; import { IDLE } from "~/client/status"; @@ -220,6 +221,9 @@ export default function ProposalPage() { const PageContent = () => { return ( <> + { - const explanation = _("Uses Btrfs for the root file system allowing to boot to a previous \ + const explanation = _("Uses Btrfs for the root file system allowing to boot to a previous \ version of the system after configuration changes or software upgrades."); - return ( - <> - -
- {explanation} -
- - ); - }; - return (
- - } /> + +
+ {explanation} +
); }; @@ -297,8 +285,6 @@ export default function ProposalSettingsSection({ encryptionMethods = [], onChange = noop }) { - const { selectedProduct } = useProduct(); - const changeEncryption = ({ password, method }) => { onChange({ encryptionPassword: password, encryptionMethod: method }); }; @@ -318,29 +304,12 @@ export default function ProposalSettingsSection({ const encryption = settings.encryptionPassword !== undefined && settings.encryptionPassword.length > 0; - const transactional = isTransactionalSystem(settings?.volumes || []); - return ( <>
- - -
- {/* TRANSLATORS: %s is replaced by a product name (e.g., openSUSE Tumbleweed) */} - {sprintf(_("%s is an immutable system with atomic updates using a read-only Btrfs \ -root file system."), selectedProduct.name)} -
- - } - else={ - - } + { }; }); -jest.mock("~/context/product", () => ({ - ...jest.requireActual("~/context/product"), - useProduct: () => ({ - selectedProduct : { name: "Test" } - }) -})); - let props; beforeEach(() => { @@ -48,7 +41,7 @@ beforeEach(() => { const rootVolume = { mountPath: "/", fsType: "Btrfs", outline: { snapshotsConfigurable: true } }; -describe("if the system is not transactional", () => { +describe("if snapshots are configurable", () => { beforeEach(() => { props.settings = { volumes: [rootVolume] }; }); @@ -60,15 +53,15 @@ describe("if the system is not transactional", () => { }); }); -describe("if the system is transactional", () => { +describe("if snapshots are not configurable", () => { beforeEach(() => { - props.settings = { volumes: [{ ...rootVolume, transactional: true }] }; + props.settings = { volumes: [{ ...rootVolume, outline: { ...rootVolume.outline, snapshotsConfigurable: false } }] }; }); - it("renders explanation about transactional system", () => { + it("does not render the snapshots switch", () => { plainRender(); - screen.getByText("Transactional system"); + expect(screen.queryByRole("checkbox", { name: "Use Btrfs Snapshots" })).toBeNull(); }); }); diff --git a/web/src/components/storage/ProposalTransactionalInfo.jsx b/web/src/components/storage/ProposalTransactionalInfo.jsx new file mode 100644 index 0000000000..aa20477a6d --- /dev/null +++ b/web/src/components/storage/ProposalTransactionalInfo.jsx @@ -0,0 +1,54 @@ +/* + * Copyright (c) [2024] SUSE LLC + * + * All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, contact SUSE LLC. + * + * To contact SUSE LLC about this file by physical or electronic mail, you may + * find current contact information at www.suse.com. + */ + +import React from "react"; + +import { sprintf } from "sprintf-js"; +import { _ } from "~/i18n"; +import { Reminder } from "~/components/core"; +import { isTransactionalSystem } from "~/components/storage/utils"; +import { useProduct } from "~/context/product"; + +/** + * @typedef {import ("~/client/storage").ProposalManager.ProposalSettings} ProposalSettings + */ + +/** + * Information about the system being transactional, if needed + * @component + * + * @param {object} props + * @param {ProposalSettings} props.settings - Settings used for calculating a proposal. + */ +export default function ProposalTransactionalInfo({ settings }) { + const { selectedProduct } = useProduct(); + + if (!isTransactionalSystem(settings?.volumes)) return; + + const title = _("Transactional root file system"); + /* TRANSLATORS: %s is replaced by a product name (e.g., openSUSE Tumbleweed) */ + const description = sprintf( + _("%s is an immutable system with atomic updates. It uses a read-only Btrfs file system updated via snapshots."), + selectedProduct.name + ); + + return {description}; +} diff --git a/web/src/components/storage/ProposalTransactionalInfo.test.jsx b/web/src/components/storage/ProposalTransactionalInfo.test.jsx new file mode 100644 index 0000000000..e9556107fa --- /dev/null +++ b/web/src/components/storage/ProposalTransactionalInfo.test.jsx @@ -0,0 +1,63 @@ +/* + * Copyright (c) [2024] SUSE LLC + * + * All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, contact SUSE LLC. + * + * To contact SUSE LLC about this file by physical or electronic mail, you may + * find current contact information at www.suse.com. + */ + +import React from "react"; +import { screen } from "@testing-library/react"; +import { plainRender } from "~/test-utils"; +import { ProposalTransactionalInfo } from "~/components/storage"; + +jest.mock("~/context/product", () => ({ + ...jest.requireActual("~/context/product"), + useProduct: () => ({ + selectedProduct : { name: "Test" } + }) +})); + +let props; + +beforeEach(() => { + props = {}; +}); + +const rootVolume = { mountPath: "/", fsType: "Btrfs" }; + +describe("if the system is not transactional", () => { + beforeEach(() => { + props.settings = { volumes: [rootVolume] }; + }); + + it("renders nothing", () => { + const { container } = plainRender(); + expect(container).toBeEmptyDOMElement(); + }); +}); + +describe("if the system is transactional", () => { + beforeEach(() => { + props.settings = { volumes: [{ ...rootVolume, transactional: true }] }; + }); + + it("renders an explanation about the transactional system", () => { + plainRender(); + + screen.getByText("Transactional root file system"); + }); +}); diff --git a/web/src/components/storage/ProposalVolumes.jsx b/web/src/components/storage/ProposalVolumes.jsx index be080ece93..c7254ed6b5 100644 --- a/web/src/components/storage/ProposalVolumes.jsx +++ b/web/src/components/storage/ProposalVolumes.jsx @@ -46,16 +46,16 @@ import { noop } from "~/utils"; * @returns {(ReactComponent|null)} component to display (can be `null`) */ const AutoCalculatedHint = (volume) => { - // no hint, the size is not affected by snapshots or other volumes - const { snapshotsAffectSizes = false, sizeRelevantVolumes = [] } = volume.outline; + const { snapshotsAffectSizes = false, sizeRelevantVolumes = [], adjustByRam } = volume.outline; - if (!snapshotsAffectSizes && sizeRelevantVolumes.length === 0) { + // no hint, the size is not affected by known criteria + if (!snapshotsAffectSizes && !adjustByRam && sizeRelevantVolumes.length === 0) { return null; } return ( <> - {/* TRANSLATORS: header for a list of items */} + {/* TRANSLATORS: header for a list of items referring to size limits for file systems */} {_("These limits are affected by:")} {snapshotsAffectSizes && @@ -65,6 +65,10 @@ const AutoCalculatedHint = (volume) => { // TRANSLATORS: list item, this affects the computed partition size limits // %s is replaced by a list of the volumes (like "/home, /boot") {sprintf(_("Presence of other volumes (%s)"), sizeRelevantVolumes.join(", "))}} + {adjustByRam && + // TRANSLATORS: list item, describes a factor that affects the computed size of a + // file system; eg. adjusting the size of the swap + {_("The amount of RAM in the system")}} ); diff --git a/web/src/components/storage/VolumeForm.jsx b/web/src/components/storage/VolumeForm.jsx index b93f3bae11..b106d9925d 100644 --- a/web/src/components/storage/VolumeForm.jsx +++ b/web/src/components/storage/VolumeForm.jsx @@ -310,6 +310,10 @@ const SizeAuto = ({ volume }) => { // TRANSLATORS: conjunction for merging two list items volume.outline.sizeRelevantVolumes.join(_(", ")))); + if (volume.outline.adjustByRam) + // TRANSLATORS: item which affects the final computed partition size + conditions.push(_("the amount of RAM in the system")); + // TRANSLATORS: the %s is replaced by the items which affect the computed size const conditionsText = sprintf(_("The final size depends on %s."), // TRANSLATORS: conjunction for merging two texts diff --git a/web/src/components/storage/index.js b/web/src/components/storage/index.js index 6c518c51a0..d4dd30a180 100644 --- a/web/src/components/storage/index.js +++ b/web/src/components/storage/index.js @@ -26,6 +26,7 @@ export { default as ProposalSpacePolicySection } from "./ProposalSpacePolicySect export { default as ProposalDeviceSection } from "./ProposalDeviceSection"; export { default as ProposalFileSystemsSection } from "./ProposalFileSystemsSection"; export { default as ProposalActionsSection } from "./ProposalActionsSection"; +export { default as ProposalTransactionalInfo } from "./ProposalTransactionalInfo"; export { default as ProposalVolumes } from "./ProposalVolumes"; export { default as DASDPage } from "./DASDPage"; export { default as DASDTable } from "./DASDTable"; diff --git a/web/src/components/storage/utils.js b/web/src/components/storage/utils.js index d2322bc167..082756b08c 100644 --- a/web/src/components/storage/utils.js +++ b/web/src/components/storage/utils.js @@ -180,8 +180,12 @@ const isTransactionalRoot = (volume) => { * @param {Volume[]} volumes * @returns {boolean} */ -const isTransactionalSystem = (volumes) => { - return volumes.find(v => isTransactionalRoot(v)) !== undefined; +const isTransactionalSystem = (volumes = []) => { + try { + return volumes?.find(v => isTransactionalRoot(v)) !== undefined; + } catch { + return false; + } }; export { diff --git a/web/src/components/storage/utils.test.js b/web/src/components/storage/utils.test.js index a973e27a0a..e5eb3cf0aa 100644 --- a/web/src/components/storage/utils.test.js +++ b/web/src/components/storage/utils.test.js @@ -139,6 +139,14 @@ describe("isTransactionalRoot", () => { }); describe("isTransactionalSystem", () => { + it("returns false when a list of volumes is not given", () => { + expect(isTransactionalSystem(false)).toBe(false); + expect(isTransactionalSystem(undefined)).toBe(false); + expect(isTransactionalSystem(null)).toBe(false); + expect(isTransactionalSystem([])).toBe(false); + expect(isTransactionalSystem("fake")).toBe(false); + }); + it("returns false if volumes does not include a transactional root", () => { expect(isTransactionalSystem([])).toBe(false);