Skip to content

Commit f093a8c

Browse files
committed
Separate ACL types, update workflow
Updated workflows to build and test with feature flags. Updated the CI/CD workflow consul config file. Moved ACL-related types into a new acl_types.rs to make them easier to feature-gate.
1 parent 60446a9 commit f093a8c

7 files changed

Lines changed: 138 additions & 123 deletions

File tree

.github/workflows/main.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ jobs:
1111
build:
1212
strategy:
1313
matrix:
14-
features: [""]
14+
features: ["", "acl"]
1515
runs-on: "ubuntu-latest"
1616
container: rust:1.82
1717

1818
steps:
1919
- uses: actions/checkout@v2
2020

2121
- name: Build
22-
run: cargo build ${{ matrix.features }}
22+
run: cargo build --features "${{ matrix.features }}"
2323

2424
test:
2525
strategy:
2626
matrix:
27-
features: [""]
27+
features: ["", "acl"]
2828
runs-on: "ubuntu-latest"
2929
container: rust:1.82
3030
services:
@@ -33,7 +33,7 @@ jobs:
3333
env:
3434
# We pass the config as a JSON here to simulate one service with 3 nodes.
3535
# TODO: Ideally, we should use the same setup in local environment (`testdata/config.hcl`) in GHA test.
36-
CONSUL_LOCAL_CONFIG: '{"acl": [{"default_policy": "allow", "enable_token_persistence": true, "enabled": true}], "services": [ {"address": "1.1.1.1", "checks": [], "id": "test-service-1", "name": "test-service", "port": 20001, "tags": ["first"]}, {"address": "2.2.2.2", "checks": [], "id": "test-service-2", "name": "test-service", "port": 20002, "tags": ["second"]}, {"address": "3.3.3.3", "checks": [], "id": "test-service-3", "name": "test-service", "port": 20003, "tags": ["third"]} ]}'
36+
CONSUL_LOCAL_CONFIG: '{"acl":[{"enabled":true,"default_policy":"allow","enable_token_persistence":true,"tokens":[{"initial_management":"8fc9e787-674f-0709-cfd5-bfdabd73a70d"}]}],"services":[{"id":"test-service-1","name":"test-service","address":"1.1.1.1","port":20001,"checks":[],"tags":["first"]},{"id":"test-service-2","name":"test-service","address":"2.2.2.2","port":20002,"checks":[],"tags":["second"]},{"id":"test-service-3","name":"test-service","address":"3.3.3.3","port":20003,"checks":[],"tags":["third"]}]}'
3737

3838
env:
3939
CONSUL_HTTP_ADDR: http://consul:8500
@@ -42,4 +42,4 @@ jobs:
4242
- uses: actions/checkout@v2
4343

4444
- name: Test
45-
run: cargo test ${{ matrix.features }}
45+
run: cargo test --features "${{ matrix.features }}"

.github/workflows/publish.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ jobs:
1313
env:
1414
# We pass the config as a JSON here to simulate one service with 3 nodes.
1515
# TODO: Ideally, we should use the same setup in local environment (`testdata/config.hcl`) in GHA test.
16-
CONSUL_LOCAL_CONFIG: '{"acl": [{"default_policy": "allow", "enable_token_persistence": true, "enabled": true}], "services": [ {"address": "1.1.1.1", "checks": [], "id": "test-service-1", "name": "test-service", "port": 20001, "tags": ["first"]}, {"address": "2.2.2.2", "checks": [], "id": "test-service-2", "name": "test-service", "port": 20002, "tags": ["second"]}, {"address": "3.3.3.3", "checks": [], "id": "test-service-3", "name": "test-service", "port": 20003, "tags": ["third"]} ]}'
16+
CONSUL_LOCAL_CONFIG: '{"acl":[{"enabled":true,"default_policy":"allow","enable_token_persistence":true,"tokens":[{"initial_management":"8fc9e787-674f-0709-cfd5-bfdabd73a70d"}]}],"services":[{"id":"test-service-1","name":"test-service","address":"1.1.1.1","port":20001,"checks":[],"tags":["first"]},{"id":"test-service-2","name":"test-service","address":"2.2.2.2","port":20002,"checks":[],"tags":["second"]},{"id":"test-service-3","name":"test-service","address":"3.3.3.3","port":20003,"checks":[],"tags":["third"]}]}'
1717
env:
1818
CONSUL_HTTP_ADDR: http://consul:8500
19-
19+
strategy:
20+
matrix:
21+
features: ["", "acl"]
2022
steps:
2123
- uses: actions/checkout@v2
2224

23-
- name: Test
24-
run: cargo test
25+
- name: Tests
26+
run: cargo test --features "${{ matrix.features }}"
2527

2628
dry-run:
2729
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ required-features = ["acl"]
6464

6565
[[example]]
6666
name = "get_acl_tokens"
67-
path = "examples/get_acl_policies.rs"
67+
path = "examples/get_acl_tokens.rs"
6868
required-features = ["acl"]
6969

7070
[[example]]
7171
name = "read_token"
72-
path = "examples/get_acl_policies.rs"
72+
path = "examples/read_token.rs"
7373
required-features = ["acl"]

src/acl.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
use std::time::Duration;
22

33
use crate::errors::ConsulError;
4-
use crate::types::ACLPolicy;
5-
use crate::types::ACLToken;
6-
use crate::types::CreateACLPolicyRequest;
4+
use crate::ACLPolicy;
5+
use crate::ACLToken;
76
use crate::Consul;
7+
use crate::CreateACLPolicyRequest;
88
use crate::CreateACLTokenPayload;
99
use crate::Function;
1010
use crate::Result;
11+
1112
use http::Method;
1213
use http_body_util::combinators::BoxBody;
1314
use http_body_util::Empty;
1415
use http_body_util::Full;
16+
1517
use hyper::body::Buf;
1618
use hyper::body::Bytes;
19+
1720
impl Consul {
1821
/// Returns all ACL tokens.
1922
///

src/acl_types.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use std::time::Duration;
2+
3+
use serde::{self, Deserialize, Serialize};
4+
5+
/// Information related ACL token.
6+
/// See https://developer.hashicorp.com/consul/docs/security/acl/tokens for more information.
7+
#[derive(Debug, Serialize, Deserialize)]
8+
#[serde(rename_all = "PascalCase")]
9+
pub struct ACLToken {
10+
/// Unique ID
11+
#[serde(rename = "AccessorID")]
12+
pub accessor_id: String,
13+
/// Secret for authenticatioIDn
14+
#[serde(rename = "SecretID")]
15+
pub secret_id: String,
16+
/// Description
17+
pub description: String,
18+
/// Policies
19+
pub policies: Option<Vec<ACLTokenPolicyLink>>,
20+
/// Token only valid in this datacenter
21+
#[serde(default)]
22+
pub local: bool,
23+
/// creation time
24+
pub create_time: String,
25+
/// hash
26+
pub hash: String,
27+
/// create index
28+
pub create_index: u64,
29+
/// ModifyIndex is the last index that modified this key.
30+
/// It can be used to establish blocking queries by setting the ?index query parameter.
31+
pub modify_index: i64,
32+
}
33+
34+
/// Information related to Policies
35+
/// see https://developer.hashicorp.com/consul/docs/security/acl/acl-policies for more information
36+
#[derive(Debug, Serialize, Deserialize, Default)]
37+
#[serde(rename_all = "PascalCase")]
38+
pub struct ACLTokenPolicyLink {
39+
/// Policy ID
40+
#[serde(rename = "ID")]
41+
pub id: Option<String>,
42+
/// Policy name
43+
pub name: Option<String>,
44+
}
45+
46+
/// Create ACL token payload
47+
/// See https://developer.hashicorp.com/consul/api-docs/acl/tokens for more information.
48+
/// todo(): NodeIdentities,TemplatedPolicies, ServiceIdentities
49+
#[derive(Debug, Serialize, Deserialize, Default)]
50+
#[serde(rename_all = "PascalCase")]
51+
pub struct CreateACLTokenPayload {
52+
/// Unique ID
53+
#[serde(rename = "AccessorID")]
54+
#[serde(skip_serializing_if = "Option::is_none")]
55+
pub accessor_id: Option<String>,
56+
/// Secret for authenticatioIDn
57+
#[serde(rename = "SecretID")]
58+
#[serde(skip_serializing_if = "Option::is_none")]
59+
pub secret_id: Option<String>,
60+
/// Description
61+
#[serde(skip_serializing_if = "Option::is_none")]
62+
pub description: Option<String>,
63+
/// Policies
64+
#[serde(skip_serializing_if = "Option::is_none")]
65+
pub policies: Option<Vec<ACLTokenPolicyLink>>,
66+
/// Token only valid in this datacenter
67+
#[serde(default)]
68+
pub local: bool,
69+
/// creation time
70+
#[serde(skip_serializing_if = "Option::is_none")]
71+
pub create_time: Option<String>,
72+
/// hash
73+
#[serde(skip_serializing_if = "Option::is_none")]
74+
pub hash: Option<String>,
75+
/// duration
76+
#[serde(skip_serializing_if = "Option::is_none")]
77+
pub expiration_time: Option<Duration>,
78+
}
79+
80+
/// Acl Policy
81+
#[derive(Debug, Serialize, Deserialize)]
82+
#[serde(rename_all = "PascalCase")]
83+
pub struct ACLPolicy {
84+
/// id
85+
#[serde(rename = "ID")]
86+
pub id: String,
87+
/// name
88+
pub name: String,
89+
/// Description
90+
pub description: String,
91+
/// hash
92+
pub hash: String,
93+
/// Create index
94+
pub create_index: u32,
95+
/// Datacenters
96+
pub datacenters: Option<String>,
97+
/// modify index
98+
pub modify_index: u32,
99+
}
100+
101+
/// Payload to create an ACL Policy
102+
#[derive(Debug, Serialize, Default)]
103+
#[serde(rename_all = "PascalCase")]
104+
pub struct CreateACLPolicyRequest {
105+
/// Name of the policy (unique)
106+
pub name: String,
107+
/// Description
108+
pub description: Option<String>,
109+
/// rules in HCL format
110+
// todo: Make the rules strongly typed
111+
pub rules: Option<String>,
112+
}

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ pub use types::*;
7373
#[cfg(feature = "acl")]
7474
/// Access Control List(acl) to control authentication and authorization
7575
pub mod acl;
76+
77+
#[cfg(feature = "acl")]
78+
/// Types for acl related operations
79+
pub mod acl_types;
80+
#[cfg(feature = "acl")]
81+
pub use acl_types::*;
82+
7683
/// Consul errors and Result type
7784
mod errors;
7885
#[cfg(feature = "trace")]

src/types.rs

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -458,115 +458,6 @@ pub struct Service {
458458
pub tags: Vec<String>,
459459
}
460460

461-
/// Information related ACL token.
462-
/// See https://developer.hashicorp.com/consul/docs/security/acl/tokens for more information.
463-
#[derive(Debug, Serialize, Deserialize)]
464-
#[serde(rename_all = "PascalCase")]
465-
pub struct ACLToken {
466-
/// Unique ID
467-
#[serde(rename = "AccessorID")]
468-
pub accessor_id: String,
469-
/// Secret for authenticatioIDn
470-
#[serde(rename = "SecretID")]
471-
pub secret_id: String,
472-
/// Description
473-
pub description: String,
474-
/// Policies
475-
pub policies: Option<Vec<ACLTokenPolicyLink>>,
476-
/// Token only valid in this datacenter
477-
#[serde(default)]
478-
pub local: bool,
479-
/// creation time
480-
pub create_time: String,
481-
/// hash
482-
pub hash: String,
483-
/// create index
484-
pub create_index: u64,
485-
/// ModifyIndex is the last index that modified this key.
486-
/// It can be used to establish blocking queries by setting the ?index query parameter.
487-
pub modify_index: i64,
488-
}
489-
490-
/// Information related to Policies
491-
/// see https://developer.hashicorp.com/consul/docs/security/acl/acl-policies for more information
492-
#[derive(Debug, Serialize, Deserialize, Default)]
493-
#[serde(rename_all = "PascalCase")]
494-
pub struct ACLTokenPolicyLink {
495-
/// Policy ID
496-
#[serde(rename = "ID")]
497-
pub id: Option<String>,
498-
/// Policy name
499-
pub name: Option<String>,
500-
}
501-
502-
/// Create ACL token payload
503-
/// See https://developer.hashicorp.com/consul/api-docs/acl/tokens for more information.
504-
/// todo(): NodeIdentities,TemplatedPolicies, ServiceIdentities
505-
#[derive(Debug, Serialize, Deserialize, Default)]
506-
#[serde(rename_all = "PascalCase")]
507-
pub struct CreateACLTokenPayload {
508-
/// Unique ID
509-
#[serde(rename = "AccessorID")]
510-
#[serde(skip_serializing_if = "Option::is_none")]
511-
pub accessor_id: Option<String>,
512-
/// Secret for authenticatioIDn
513-
#[serde(rename = "SecretID")]
514-
#[serde(skip_serializing_if = "Option::is_none")]
515-
pub secret_id: Option<String>,
516-
/// Description
517-
#[serde(skip_serializing_if = "Option::is_none")]
518-
pub description: Option<String>,
519-
/// Policies
520-
#[serde(skip_serializing_if = "Option::is_none")]
521-
pub policies: Option<Vec<ACLTokenPolicyLink>>,
522-
/// Token only valid in this datacenter
523-
#[serde(default)]
524-
pub local: bool,
525-
/// creation time
526-
#[serde(skip_serializing_if = "Option::is_none")]
527-
pub create_time: Option<String>,
528-
/// hash
529-
#[serde(skip_serializing_if = "Option::is_none")]
530-
pub hash: Option<String>,
531-
/// duration
532-
#[serde(skip_serializing_if = "Option::is_none")]
533-
pub expiration_time: Option<Duration>,
534-
}
535-
536-
/// Acl Policy
537-
#[derive(Debug, Serialize, Deserialize)]
538-
#[serde(rename_all = "PascalCase")]
539-
pub struct ACLPolicy {
540-
/// id
541-
#[serde(rename = "ID")]
542-
pub id: String,
543-
/// name
544-
pub name: String,
545-
/// Description
546-
pub description: String,
547-
/// hash
548-
pub hash: String,
549-
/// Create index
550-
pub create_index: u32,
551-
/// Datacenters
552-
pub datacenters: Option<String>,
553-
/// modify index
554-
pub modify_index: u32,
555-
}
556-
557-
/// Payload to create an ACL Policy
558-
#[derive(Debug, Serialize, Default)]
559-
#[serde(rename_all = "PascalCase")]
560-
pub struct CreateACLPolicyRequest {
561-
/// Name of the policy (unique)
562-
pub name: String,
563-
/// Description
564-
pub description: Option<String>,
565-
/// rules in HCL format
566-
// todo: Make the rules strongly typed
567-
pub rules: Option<String>,
568-
}
569-
570461
pub(crate) fn serialize_duration_as_string<S>(
571462
duration: &Duration,
572463
serializer: S,

0 commit comments

Comments
 (0)