Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce naming conventions #374

Merged
merged 18 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ serde_json = "1.0"
uuid = { version = "1.3", features = ["v4", "fast-rng"] }
sha256 = "1.5"
umask = "2.1.0"
regex = "1.10"

[dev-dependencies]
common = { path = "../common", features = ["test_utils"] }
Expand Down
17 changes: 16 additions & 1 deletion agent/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,31 @@
//
// SPDX-License-Identifier: Apache-2.0

use regex::Regex;
use std::path::Path;

#[cfg_attr(test, mockall_double::double)]
use crate::control_interface::Directory;
use crate::control_interface::FileSystemError;
use clap::Parser;
use common::objects::state::STR_RE_AGENT;
use common::DEFAULT_SERVER_ADDRESS;

const DEFAULT_RUN_FOLDER: &str = "/tmp/ankaios/";
const RUNFOLDER_SUFFIX: &str = "_io";

fn validate_agent_name(name: &str) -> Result<(), String> {
let re = Regex::new(STR_RE_AGENT).unwrap();
if re.is_match(name) {
Ok(())
} else {
Err(format!(
"Agent name '{}' is invalid. It shall contain only regular upper and lowercase characters (a-z and A-Z), numbers and the symbols '-' and '_'.",
name
))
}
}

// [impl->swdd~agent-supports-cli-argument-for-insecure-communication~1]
// [impl->swdd~agent-supports-pem-file-paths-as-cli-arguments~1]
#[derive(Parser, Debug)]
Expand All @@ -31,8 +45,9 @@ const RUNFOLDER_SUFFIX: &str = "_io";
about="Ankaios - your friendly automotive workload orchestrator.\nWhat can the agent do for you?")
]
pub struct Arguments {
#[clap(short = 'n', long = "name")]
#[clap(short = 'n', long = "name", value_parser = validate_agent_name)]
/// The name to use for the registration with the server. Every agent has to register with a unique name.
/// Agent name shall contain only regular upper and lowercase characters (a-z and A-Z), numbers and the symbols "-" and "_".
pub agent_name: String,
#[clap(short = 's', long = "server-url", default_value_t = DEFAULT_SERVER_ADDRESS.to_string())]
/// The server url.
Expand Down
1 change: 1 addition & 0 deletions agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ async fn main() {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));

let args = cli::parse();

let server_url = match args.insecure {
true => args.server_url.replace("http[s]", "http"),
false => args.server_url.replace("http[s]", "https"),
Expand Down
7 changes: 7 additions & 0 deletions api/proto/ank_base.proto
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ message WorkloadState {
ExecutionState executionState = 2; /// The workload execution state.
}

/**
* In case of workload names, the naming convention states that thier names shall:
* - contain only regular upper and lowercase characters (a-z and A-Z), numbers and the symbols "-" and "_"
* - have a minimal length of 1 character
* - have a maximal length of 63 characters
* Also, agent name shall contain only regular upper and lowercase characters (a-z and A-Z), numbers and the symbols "-" and "_".
*/
message WorkloadInstanceName {
krucod3 marked this conversation as resolved.
Show resolved Hide resolved
string workloadName = 1; /// The name of the workload.
string agentName = 2; /// The name of the owning Agent.
Expand Down
4 changes: 2 additions & 2 deletions common/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

// [impl->swdd~common-conversions-between-ankaios-and-proto~1]

mod state;
pub mod state;
pub use state::State;
pub use state::STR_RE_WORKLOAD;
pub use state::{STR_RE_AGENT, STR_RE_WORKLOAD};

mod complete_state;
pub use complete_state::CompleteState;
Expand Down
7 changes: 6 additions & 1 deletion common/src/objects/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use api::ank_base;
const CURRENT_API_VERSION: &str = "v0.1";
const MAX_CHARACTERS_WORKLOAD_NAME: usize = 63;
pub const STR_RE_WORKLOAD: &str = r"^[a-zA-Z0-9_-]+*$";
const STR_RE_AGENT: &str = r"^[a-zA-Z0-9_-]*$";
pub const STR_RE_AGENT: &str = r"^[a-zA-Z0-9_-]*$";

// [impl->swdd~common-object-representation~1]
// [impl->swdd~common-object-serialization~1]
Expand Down Expand Up @@ -79,6 +79,8 @@ impl TryFrom<ank_base::State> for State {
}

impl State {
// [impl->swdd~common-workload-naming-convention~1]
// [impl->swdd~common-agent-naming-convention~1]
pub fn verify_format(provided_state: &State) -> Result<(), String> {
if provided_state.api_version != CURRENT_API_VERSION {
return Err(format!(
Expand Down Expand Up @@ -196,6 +198,7 @@ mod tests {
);
}

// [utest->swdd~common-workload-naming-convention~1]
#[test]
fn utest_state_rejects_incompatible_state_on_workload_name() {
let workload_name = "nginx.test".to_string();
Expand All @@ -212,6 +215,7 @@ mod tests {
);
}

// [utest->swdd~common-workload-naming-convention~1]
#[test]
fn utest_state_rejects_incompatible_state_on_inordinately_long_workload_name() {
let workload_name = "workload_name_is_too_long_for_ankaios_to_accept_it_and_I_don_t_know_what_else_to_write".to_string();
Expand All @@ -229,6 +233,7 @@ mod tests {
);
}

// [utest->swdd~common-agent-naming-convention~1]
#[test]
fn utest_state_rejects_incompatible_state_on_agent_name() {
let agent_name = "agent_A.test".to_string();
Expand Down
8 changes: 8 additions & 0 deletions doc/docs/reference/complete-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ desiredState:
restartPolicy: ALWAYS
```

!!! Note

In case of workload names, the naming convention states that thier names shall:
* contain only regular upper and lowercase characters (a-z and A-Z), numbers and the symbols "-" and "_"
* have a minimal length of 1 character
* have a maximal length of 63 characters
Also, agent name shall contain only regular upper and lowercase characters (a-z and A-Z), numbers and the symbols "-" and "_".

## Object field mask

With the object field mask only specific parts of the Ankaios state could be retrieved or updated.
Expand Down
8 changes: 8 additions & 0 deletions doc/docs/usage/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ delete it from the state again with:
ank -k delete workload helloworld
```

!!! Note

In case of workload names, the naming convention states that thier names shall:
* contain only regular upper and lowercase characters (a-z and A-Z), numbers and the symbols "-" and "_"
* have a minimal length of 1 character
* have a maximal length of 63 characters
Also, agent name shall contain only regular upper and lowercase characters (a-z and A-Z), numbers and the symbols "-" and "_".

HorjuRares marked this conversation as resolved.
Show resolved Hide resolved
For next steps follow the [tutorial on sending and receiving vehicle data](tutorial-vehicle-signals.md) with workloads orchestrated by Ankaios.
Then also check the reference documentation for the
[startup configuration](../reference/startup-configuration.md) including the
Expand Down
2 changes: 2 additions & 0 deletions server/src/ankaios_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ impl AnkaiosServer {

// [impl->swdd~update-desired-state-with-invalid-version~1]
// [impl->swdd~update-desired-state-with-missing-version~1]
// [impl->swdd~common-workload-naming-convention~1]
// [impl->swdd~common-agent-naming-convention~1]
krucod3 marked this conversation as resolved.
Show resolved Hide resolved
if let Err(error_message) =
State::verify_format(&update_state_request.state.desired_state)
{
Expand Down
3 changes: 3 additions & 0 deletions tests/stests/workloads/update_workload_podman.robot
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Test Ankaios Podman Update workload with invalid api version

[Teardown] Clean up Ankaios

# [stest->swdd~common-workload-naming-convention~1]
Test Ankaios Podman Update workload with invalid workload name
[Setup] Run Keywords Setup Ankaios

Expand All @@ -100,6 +101,7 @@ Test Ankaios Podman Update workload with invalid workload name

[Teardown] Clean up Ankaios

# [stest->swdd~common-workload-naming-convention~1]
Test Ankaios Podman Update workload with lengthy workload name
[Setup] Run Keywords Setup Ankaios

Expand All @@ -117,6 +119,7 @@ Test Ankaios Podman Update workload with lengthy workload name

[Teardown] Clean up Ankaios

# [stest->swdd~common-agent-naming-convention~1]
Test Ankaios Podman Update workload with invalid agent name
[Setup] Run Keywords Setup Ankaios

Expand Down