Skip to content

Commit 62fe521

Browse files
committed
feat: add --insecure flag to skip SSL certificate verification for API requests
1 parent 9e0d092 commit 62fe521

File tree

12 files changed

+98
-24
lines changed

12 files changed

+98
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Here are the planned enhancements and features for WRPT:
7474
| -l | `--url <URL>` | URL of the Portainer instance. |
7575
| -A | `--access-token <ACCESS_TOKEN>` | Access token of the Portainer instance. Learn how to generate an access token [here](https://docs.portainer.io/api/access#creating-an-access-token). |
7676
| | `--color <COLOR>` | When to use terminal colours [default: auto] [possible values: auto, always, never]. |
77+
| | `--insecure` | Skip the host's SSL certificate verification, use at your own risk. |
7778
| -v... | | Increase the verbosity of messages: 1 for normal output, 2 for more verbose output, 3 for debug and 4 for trace. |
7879
| -q | `--quiet` | Do not output any message. |
7980
| -h | `--help` | Print help. |

src/commands/endpoints/handlers/list.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,28 @@ pub(crate) fn handler(command: EndpointListCommand, global_args: GlobalArgs) ->
1414
let base_url = get_base_url(&global_args)?;
1515
let access_token = get_access_token(&global_args)?;
1616

17-
let endpoints = fetch_endpoints(base_url.as_str(), access_token.as_str())?;
17+
let endpoints = fetch_endpoints(
18+
base_url.as_str(),
19+
access_token.as_str(),
20+
global_args.insecure,
21+
)?;
1822

1923
build_table(&endpoints, None).printstd();
2024

2125
Ok(())
2226
}
2327

24-
pub(crate) fn fetch_endpoints(base_url: &str, access_token: &str) -> Result<Vec<EndpointList>, ()> {
28+
pub(crate) fn fetch_endpoints(
29+
base_url: &str,
30+
access_token: &str,
31+
insecure: bool,
32+
) -> Result<Vec<EndpointList>, ()> {
2533
let url =
2634
construct_url(base_url, consts::ENDPOINT_ENDPOINTS).log_expect("failed to construct url");
2735

2836
debug!("request = GET {:?}", url.as_str());
2937

30-
let response = create_client(access_token)
38+
let response = create_client(access_token, insecure)
3139
.get(url)
3240
.query(&[("excludeSnapshots", "true")])
3341
.send()

src/commands/helpers.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ use std::fs::File;
1616
use std::io::{self, BufRead};
1717
use std::path::PathBuf;
1818

19-
pub(crate) fn create_client(api_key: &str) -> reqwest::blocking::Client {
19+
pub(crate) fn create_client(api_key: &str, insecure: bool) -> reqwest::blocking::Client {
2020
reqwest::blocking::Client::builder()
21+
.danger_accept_invalid_certs(insecure)
2122
.default_headers({
2223
let mut headers = reqwest::header::HeaderMap::new();
2324
headers.insert(
@@ -34,8 +35,9 @@ pub(crate) fn get_stack_id_from_name(
3435
name: &str,
3536
base_url: &str,
3637
access_token: &str,
38+
insecure: bool,
3739
) -> Result<Option<u32>, ()> {
38-
let stacks = fetch_stacks(base_url, access_token)?;
40+
let stacks = fetch_stacks(base_url, access_token, insecure)?;
3941

4042
for stack in stacks {
4143
if stack.name.eq(name) {
@@ -50,6 +52,7 @@ pub(crate) fn get_swarm_id_from_endpoint_id(
5052
endpoint_id: u32,
5153
url: &str,
5254
access_token: &str,
55+
insecure: bool,
5356
) -> Option<String> {
5457
let mut url = url.to_string();
5558
url.push_str(
@@ -58,7 +61,7 @@ pub(crate) fn get_swarm_id_from_endpoint_id(
5861
.as_str(),
5962
);
6063

61-
let response = create_client(access_token).get(url).send();
64+
let response = create_client(access_token, insecure).get(url).send();
6265

6366
let body = response
6467
.log_expect("invalid response from API")

src/commands/stacks/handlers/deploy.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub(crate) fn handler(command: StackDeployCommand, global_args: GlobalArgs) -> R
3131
command.stack_name.as_str(),
3232
base_url.as_str(),
3333
access_token.as_str(),
34+
global_args.insecure,
3435
)?;
3536

3637
let stack: Vec<Stack> = if stack_id.is_none() {
@@ -41,6 +42,7 @@ pub(crate) fn handler(command: StackDeployCommand, global_args: GlobalArgs) -> R
4142
command.endpoint,
4243
base_url.as_str(),
4344
access_token.as_str(),
45+
global_args.insecure,
4446
);
4547

4648
match swarm_id {
@@ -64,6 +66,7 @@ pub(crate) fn handler(command: StackDeployCommand, global_args: GlobalArgs) -> R
6466
stack_create_payload,
6567
command.endpoint,
6668
consts::ENDPOINT_STACKS_CREATE_SWARM_STRING,
69+
global_args.insecure,
6770
)?
6871
}
6972
None => {
@@ -85,6 +88,7 @@ pub(crate) fn handler(command: StackDeployCommand, global_args: GlobalArgs) -> R
8588
stack_create_payload,
8689
command.endpoint,
8790
consts::ENDPOINT_STACKS_CREATE_STANDALONE_STRING,
91+
global_args.insecure,
8892
)?
8993
}
9094
}
@@ -111,6 +115,7 @@ pub(crate) fn handler(command: StackDeployCommand, global_args: GlobalArgs) -> R
111115
stack_update_payload,
112116
stack_id.unwrap_or_default(),
113117
command.endpoint,
118+
global_args.insecure,
114119
)?
115120
};
116121

@@ -143,12 +148,13 @@ pub(crate) fn create_stack<T: serde::Serialize>(
143148
stack_create_payload: T,
144149
entrypoint_id: u32,
145150
endpoint: &str,
151+
insecure: bool,
146152
) -> Result<Vec<Stack>, ()> {
147153
let url = construct_url(base_url, endpoint).log_expect("failed to construct url");
148154

149155
debug!("request = POST {:?}", url.as_str());
150156

151-
let response = create_client(access_token)
157+
let response = create_client(access_token, insecure)
152158
.post(url)
153159
.json(&stack_create_payload)
154160
.query(&[("endpointId", entrypoint_id)])
@@ -164,6 +170,7 @@ pub(crate) fn update_stack(
164170
stack_update_payload: StackDeployUpdatePayload,
165171
stack_id: u32,
166172
entrypoint_id: u32,
173+
insecure: bool,
167174
) -> Result<Vec<Stack>, ()> {
168175
let url = construct_url(
169176
base_url,
@@ -175,7 +182,7 @@ pub(crate) fn update_stack(
175182

176183
debug!("request = PUT {:?}", url.as_str());
177184

178-
let response = create_client(access_token)
185+
let response = create_client(access_token, insecure)
179186
.put(url)
180187
.json(&stack_update_payload)
181188
.query(&[("endpointId", entrypoint_id)])

src/commands/stacks/handlers/list.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ pub(crate) fn handler(command: StackListCommand, global_args: GlobalArgs) -> Res
1414
let base_url = get_base_url(&global_args)?;
1515
let access_token = get_access_token(&global_args)?;
1616

17-
let stacks = fetch_stacks(base_url.as_str(), access_token.as_str())?;
17+
let stacks = fetch_stacks(
18+
base_url.as_str(),
19+
access_token.as_str(),
20+
global_args.insecure,
21+
)?;
1822

1923
build_table(
2024
&stacks,
@@ -37,13 +41,17 @@ pub(crate) fn handler(command: StackListCommand, global_args: GlobalArgs) -> Res
3741
Ok(())
3842
}
3943

40-
pub(crate) fn fetch_stacks(base_url: &str, access_token: &str) -> Result<Vec<StackList>, ()> {
44+
pub(crate) fn fetch_stacks(
45+
base_url: &str,
46+
access_token: &str,
47+
insecure: bool,
48+
) -> Result<Vec<StackList>, ()> {
4149
let url =
4250
construct_url(base_url, consts::ENDPOINT_STACKS).log_expect("failed to construct url");
4351

4452
debug!("request = GET {:?}", url.as_str());
4553

46-
let response = create_client(access_token)
54+
let response = create_client(access_token, insecure)
4755
.get(url)
4856
.send()
4957
.log_expect("invalid response from API");

src/commands/stacks/handlers/remove.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub(crate) fn handler(command: StackRemoveCommand, global_args: GlobalArgs) -> R
1919
command.stack_name.as_str(),
2020
base_url.as_str(),
2121
access_token.as_str(),
22+
global_args.insecure,
2223
)?;
2324

2425
if stack_id.is_none() {
@@ -39,14 +40,21 @@ pub(crate) fn handler(command: StackRemoveCommand, global_args: GlobalArgs) -> R
3940
access_token.as_str(),
4041
stack_id.unwrap_or_default(),
4142
command.endpoint,
43+
global_args.insecure,
4244
);
4345

4446
info!("Done");
4547

4648
Ok(())
4749
}
4850

49-
pub(crate) fn remove_stack(base_url: &str, access_token: &str, stack_id: u32, entrypoint_id: u32) {
51+
pub(crate) fn remove_stack(
52+
base_url: &str,
53+
access_token: &str,
54+
stack_id: u32,
55+
entrypoint_id: u32,
56+
insecure: bool,
57+
) {
5058
let url = construct_url(
5159
base_url,
5260
consts::ENDPOINT_STACKS_REMOVE
@@ -57,7 +65,7 @@ pub(crate) fn remove_stack(base_url: &str, access_token: &str, stack_id: u32, en
5765

5866
debug!("request = DELETE {:?}", url.as_str());
5967

60-
let response = create_client(access_token)
68+
let response = create_client(access_token, insecure)
6169
.delete(url)
6270
.query(&[("endpointId", entrypoint_id)])
6371
.send()

src/commands/stacks/handlers/resource_control.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub(crate) fn handler(
2323
command.stack_name.as_str(),
2424
base_url.as_str(),
2525
access_token.as_str(),
26+
global_args.insecure,
2627
)?;
2728

2829
if stack_id.is_none() {
@@ -46,6 +47,7 @@ pub(crate) fn handler(
4647
access_token.as_str(),
4748
stack_id.unwrap_or_default(),
4849
command.endpoint,
50+
global_args.insecure,
4951
)?;
5052

5153
let resource_control = &stack.first().ok_or(())?.resource_control;
@@ -60,6 +62,7 @@ pub(crate) fn inspect_stack(
6062
access_token: &str,
6163
stack_id: u32,
6264
entrypoint_id: u32,
65+
insecure: bool,
6366
) -> Result<Vec<Stack>, ()> {
6467
let url = construct_url(
6568
base_url,
@@ -71,7 +74,7 @@ pub(crate) fn inspect_stack(
7174

7275
debug!("request = GET {:?}", url.as_str());
7376

74-
let response = create_client(access_token)
77+
let response = create_client(access_token, insecure)
7578
.get(url)
7679
.query(&[("endpointId", entrypoint_id)])
7780
.send()

src/commands/stacks/handlers/start.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub(crate) fn handler(command: StackStartCommand, global_args: GlobalArgs) -> Re
1919
command.stack_name.as_str(),
2020
base_url.as_str(),
2121
access_token.as_str(),
22+
global_args.insecure,
2223
)?;
2324

2425
if stack_id.is_none() {
@@ -39,14 +40,21 @@ pub(crate) fn handler(command: StackStartCommand, global_args: GlobalArgs) -> Re
3940
access_token.as_str(),
4041
stack_id.unwrap_or_default(),
4142
command.endpoint,
43+
global_args.insecure,
4244
);
4345

4446
info!("Done");
4547

4648
Ok(())
4749
}
4850

49-
pub(crate) fn start_stack(base_url: &str, access_token: &str, stack_id: u32, entrypoint_id: u32) {
51+
pub(crate) fn start_stack(
52+
base_url: &str,
53+
access_token: &str,
54+
stack_id: u32,
55+
entrypoint_id: u32,
56+
insecure: bool,
57+
) {
5058
let url = construct_url(
5159
base_url,
5260
consts::ENDPOINT_STACKS_START
@@ -57,7 +65,7 @@ pub(crate) fn start_stack(base_url: &str, access_token: &str, stack_id: u32, ent
5765

5866
debug!("request = POST {:?}", url.as_str());
5967

60-
let response = create_client(access_token)
68+
let response = create_client(access_token, insecure)
6169
.post(url)
6270
.query(&[("endpointId", entrypoint_id)])
6371
.send()

src/commands/stacks/handlers/stop.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub(crate) fn handler(command: StackStopCommand, global_args: GlobalArgs) -> Res
1919
command.stack_name.as_str(),
2020
base_url.as_str(),
2121
access_token.as_str(),
22+
global_args.insecure,
2223
)?;
2324

2425
if stack_id.is_none() {
@@ -39,14 +40,21 @@ pub(crate) fn handler(command: StackStopCommand, global_args: GlobalArgs) -> Res
3940
access_token.as_str(),
4041
stack_id.unwrap_or_default(),
4142
command.endpoint,
43+
global_args.insecure,
4244
);
4345

4446
info!("Done");
4547

4648
Ok(())
4749
}
4850

49-
pub(crate) fn stop_stack(base_url: &str, access_token: &str, stack_id: u32, entrypoint_id: u32) {
51+
pub(crate) fn stop_stack(
52+
base_url: &str,
53+
access_token: &str,
54+
stack_id: u32,
55+
entrypoint_id: u32,
56+
insecure: bool,
57+
) {
5058
let url = construct_url(
5159
base_url,
5260
consts::ENDPOINT_STACKS_STOP
@@ -57,7 +65,7 @@ pub(crate) fn stop_stack(base_url: &str, access_token: &str, stack_id: u32, entr
5765

5866
debug!("request = POST {:?}", url.as_str());
5967

60-
let response = create_client(access_token)
68+
let response = create_client(access_token, insecure)
6169
.post(url)
6270
.query(&[("endpointId", entrypoint_id)])
6371
.send()

src/commands/teams/handlers/list.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,27 @@ pub(crate) fn handler(command: TeamListCommand, global_args: GlobalArgs) -> Resu
1414
let base_url = get_base_url(&global_args)?;
1515
let access_token = get_access_token(&global_args)?;
1616

17-
let teams = fetch_teams(base_url.as_str(), access_token.as_str())?;
17+
let teams = fetch_teams(
18+
base_url.as_str(),
19+
access_token.as_str(),
20+
global_args.insecure,
21+
)?;
1822

1923
build_table(&teams, None).printstd();
2024

2125
Ok(())
2226
}
2327

24-
pub(crate) fn fetch_teams(base_url: &str, access_token: &str) -> Result<Vec<TeamList>, ()> {
28+
pub(crate) fn fetch_teams(
29+
base_url: &str,
30+
access_token: &str,
31+
insecure: bool,
32+
) -> Result<Vec<TeamList>, ()> {
2533
let url = construct_url(base_url, consts::ENDPOINT_TEAMS).log_expect("failed to construct url");
2634

2735
debug!("request = GET {:?}", url.as_str());
2836

29-
let response = create_client(access_token)
37+
let response = create_client(access_token, insecure)
3038
.get(url)
3139
.send()
3240
.log_expect("invalid response from API");

0 commit comments

Comments
 (0)