Skip to main content

rs_consul/
acl_types.rs

1use serde::{self, Deserialize, Serialize};
2
3/// Information related ACL token.
4/// See https://developer.hashicorp.com/consul/docs/security/acl/tokens for more information.
5#[derive(Debug, Serialize, Deserialize)]
6#[serde(rename_all = "PascalCase")]
7pub struct ACLToken {
8    /// Unique ID
9    #[serde(rename = "AccessorID")]
10    pub accessor_id: String,
11    /// Secret for authentication
12    #[serde(rename = "SecretID")]
13    pub secret_id: String,
14    /// Description
15    pub description: String,
16    /// Policies
17    #[serde(default)]
18    pub policies: Vec<ACLTokenPolicyLink>,
19    /// Token only valid in this datacenter
20    #[serde(default)]
21    pub local: bool,
22    /// Creation time
23    pub create_time: String,
24    /// Hash
25    pub hash: String,
26    /// Create index
27    pub create_index: u64,
28    /// ModifyIndex is the last index that modified this key.
29    /// It can be used to establish blocking queries by setting the ?index query parameter.
30    pub modify_index: i64,
31}
32
33/// Information related to Policies
34/// see https://developer.hashicorp.com/consul/docs/security/acl/acl-policies for more information
35#[derive(Debug, Serialize, Deserialize, Default, bon::Builder)]
36#[serde(rename_all = "PascalCase")]
37pub struct ACLTokenPolicyLink {
38    /// Policy ID
39    #[serde(rename = "ID")]
40    pub id: Option<String>,
41    /// Policy name
42    pub name: Option<String>,
43}
44
45/// Create ACL token payload
46/// See https://developer.hashicorp.com/consul/api-docs/acl/tokens for more information.
47/// TODO: NodeIdentities,TemplatedPolicies, ServiceIdentities
48#[derive(Debug, Serialize, Deserialize, Default, bon::Builder)]
49#[serde(rename_all = "PascalCase")]
50pub struct CreateACLTokenPayload {
51    /// Unique ID
52    #[serde(rename = "AccessorID")]
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub accessor_id: Option<String>,
55    /// Secret for authentication
56    #[serde(rename = "SecretID")]
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub secret_id: Option<String>,
59    /// Description
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub description: Option<String>,
62    /// Policies
63    #[serde(skip_serializing_if = "Vec::is_empty")]
64    pub policies: Vec<ACLTokenPolicyLink>,
65    /// Token only valid in this datacenter
66    #[serde(default)]
67    #[builder(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    /// Optional expiration time for the ACL token.
76    ///
77    /// If set, this field specifies the point in time after which the token is considered revoked and eligible for destruction.
78    /// The default value means the token does not expire.
79    ///
80    /// The expiration time must be between 1 minute and 24 hours in the future. This constraint is enforced by Consul to promote
81    /// security best practices, ensuring that tokens are short-lived and reducing the risk of unauthorized access in case of token compromise.
82    ///
83    /// The value should be provided in RFC 3339 format, representing a date and time with optional fractional seconds and time zone offset.
84    /// For example: "2025-05-29T15:00:00Z" or "2025-05-29T15:00:00+02:00".
85    ///
86    /// Added in Consul 1.5.0.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub expiration_time: Option<String>,
89}
90
91/// Represents an ACL (Access Control List) policy.
92#[derive(Debug, Serialize, Deserialize)]
93#[serde(rename_all = "PascalCase")]
94pub struct ACLPolicy {
95    /// Unique identifier for the policy.
96    #[serde(rename = "ID")]
97    pub id: String,
98    /// The name of the policy
99    pub name: String,
100    /// Description of the policy.
101    pub description: String,
102    /// Hash of the policy.
103    pub hash: String,
104    /// Index at which the policy was created.
105    pub create_index: u32,
106    // `datacenters` is an Option::Vec because Consul may return `null` for this field.
107    // Using `Option` avoids the need for a custom deserializer that would be required
108    // if the field was just a `Vec`.
109    /// List of applicable datacenters.
110    pub datacenters: Option<Vec<String>>,
111    /// Index at which the policy was last modified.
112    pub modify_index: u32,
113}
114
115/// Payload to create an ACL Policy
116#[derive(Debug, Serialize, Default, bon::Builder)]
117#[serde(rename_all = "PascalCase")]
118pub struct CreateACLPolicyRequest {
119    /// Name of the policy (unique)
120    pub name: String,
121    /// Description
122    pub description: Option<String>,
123    /// Rules in HCL format
124    // TODO: Make the rules strongly typed
125    pub rules: Option<String>,
126}