Skip to content

Commit cdb17a7

Browse files
committed
Migrate to biscuit-auth 6.0.0
- biscuit-datalog v3.3 support - secp256r1 support - authorizers are now immutable (except for fact generation): adding code to a parsed snapshot is not possible anymore
1 parent fca8b95 commit cdb17a7

File tree

5 files changed

+273
-220
lines changed

5 files changed

+273
-220
lines changed

src/cli.rs

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use biscuit_auth::Algorithm;
12
use clap::Parser;
23
use std::path::PathBuf;
34

@@ -49,6 +50,9 @@ pub struct KeyPairCmd {
4950
/// Output the private key raw bytes directly, with no hex encoding
5051
#[clap(long, requires("only-private-key"))]
5152
pub raw_private_key_output: bool,
53+
/// Key algorithm: ed25519 (default) or secp256r1
54+
#[clap(long, default_value_t)]
55+
pub key_algorithm: Algorithm,
5256
}
5357

5458
/// Generate a biscuit from a private key and an authority block
@@ -79,6 +83,9 @@ pub struct Generate {
7983
/// Read the private key raw bytes directly (only available when reading the private key from a file)
8084
#[clap(long, conflicts_with = "private-key", requires = "private-key-file")]
8185
pub raw_private_key: bool,
86+
/// Key algorithm: ed25519 (default) or secp256r1
87+
#[clap(long, default_value_t)]
88+
pub key_algorithm: Algorithm,
8289
/// The optional context string attached to the authority block
8390
#[clap(long)]
8491
pub context: Option<String>,
@@ -142,6 +149,11 @@ pub struct Inspect {
142149
/// Read the public key raw bytes directly
143150
#[clap(long, requires("public-key-file"), conflicts_with("public-key"))]
144151
pub raw_public_key: bool,
152+
/// Key algorithm: ed25519 (default) or secp256r1
153+
#[clap(long, default_value_t)]
154+
pub key_algorithm: Algorithm,
155+
#[clap(flatten)]
156+
pub run_limits_args: common_args::RunLimitArgs,
145157
#[clap(flatten)]
146158
pub authorization_args: common_args::AuthorizeArgs,
147159
#[clap(flatten)]
@@ -169,7 +181,7 @@ pub struct InspectSnapshot {
169181
#[clap(long)]
170182
pub raw_input: bool,
171183
#[clap(flatten)]
172-
pub authorization_args: common_args::AuthorizeArgs,
184+
pub run_limits_args: common_args::RunLimitArgs,
173185
#[clap(flatten)]
174186
pub query_args: common_args::QueryArgs,
175187
#[clap(flatten)]
@@ -209,6 +221,9 @@ pub struct GenerateThirdPartyBlock {
209221
/// Read the private key raw bytes directly (only available when reading the private key from a file)
210222
#[clap(long, conflicts_with = "private-key", requires = "private-key-file")]
211223
pub raw_private_key: bool,
224+
/// Key algorithm: ed25519 (default) or secp256r1
225+
#[clap(long, default_value_t)]
226+
pub key_algorithm: Algorithm,
212227
/// Output the block raw bytes directly, with no base64 encoding
213228
#[clap(long)]
214229
pub raw_output: bool,
@@ -254,7 +269,7 @@ mod common_args {
254269
pub struct ParamArg {
255270
/// Provide a value for a datalog parameter. `type` is optional and defaults to `string`. Possible types are pubkey, string, integer, date, bytes or bool.
256271
/// Bytes values must be hex-encoded and start with `hex:`
257-
/// Public keys must be hex-encoded and start with `ed25519/`
272+
/// Public keys must be hex-encoded and start with `ed25519/` or `secp256r1/`
258273
#[clap(
259274
long,
260275
value_parser = clap::builder::ValueParser::new(parse_param),
@@ -263,6 +278,25 @@ mod common_args {
263278
pub param: Vec<Param>,
264279
}
265280

281+
/// Arguments related to runtime limits
282+
#[derive(Parser)]
283+
pub struct RunLimitArgs {
284+
/// Configure the maximum amount of facts that can be generated
285+
/// before aborting evaluation
286+
#[clap(long)]
287+
pub max_facts: Option<u64>,
288+
/// Configure the maximum amount of iterations before aborting
289+
/// evaluation
290+
#[clap(long)]
291+
pub max_iterations: Option<u64>,
292+
/// Configure the maximum evaluation duration before aborting
293+
#[clap(
294+
long,
295+
parse(try_from_str = parse_duration)
296+
)]
297+
pub max_time: Option<Duration>,
298+
}
299+
266300
/// Arguments related to running authorization
267301
#[derive(Parser)]
268302
pub struct AuthorizeArgs {
@@ -291,33 +325,6 @@ mod common_args {
291325
conflicts_with("authorize-interactive")
292326
)]
293327
pub authorize_with: Option<String>,
294-
/// Configure the maximum amount of facts that can be generated
295-
/// before aborting evaluation
296-
#[clap(
297-
long,
298-
requires("authorize-with"),
299-
requires("authorize-interactive"),
300-
requires("authorize-with-file")
301-
)]
302-
pub max_facts: Option<u64>,
303-
/// Configure the maximum amount of iterations before aborting
304-
/// evaluation
305-
#[clap(
306-
long,
307-
requires("authorize-with"),
308-
requires("authorize-interactive"),
309-
requires("authorize-with-file")
310-
)]
311-
pub max_iterations: Option<u64>,
312-
/// Configure the maximum evaluation duration before aborting
313-
#[clap(
314-
long,
315-
requires("authorize-with"),
316-
requires("authorize-interactive"),
317-
requires("authorize-with-file"),
318-
parse(try_from_str = parse_duration)
319-
)]
320-
pub max_time: Option<Duration>,
321328
/// Include the current time in the verifier facts
322329
#[clap(long)]
323330
pub include_time: bool,

src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub enum CliError {
2727
MissingPublicKeyForQuerying,
2828
#[error("Signatures check failed")]
2929
SignaturesCheckFailed,
30+
#[error("Datalog fact generation failed")]
31+
EvaluationFailed,
3032
#[error("Authorization failed")]
3133
AuthorizationFailed,
3234
#[error("Querying failed")]

src/input.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use anyhow::Result;
22
use atty::Stream;
33
use biscuit_auth::{
44
builder::{BiscuitBuilder, BlockBuilder, Rule, Term},
5-
Authorizer, ThirdPartyRequest, UnverifiedBiscuit, {PrivateKey, PublicKey},
5+
Algorithm, Authorizer, AuthorizerBuilder, PrivateKey, PublicKey, ThirdPartyRequest,
6+
UnverifiedBiscuit,
67
};
78
use chrono::{DateTime, Duration, Utc};
89
use parse_duration as duration_parser;
@@ -131,8 +132,8 @@ pub fn read_authority_from(
131132
from: &DatalogInput,
132133
all_params: &[Param],
133134
context: &Option<String>,
134-
builder: &mut BiscuitBuilder,
135-
) -> Result<()> {
135+
builder: BiscuitBuilder,
136+
) -> Result<BiscuitBuilder> {
136137
let string = match from {
137138
DatalogInput::FromEditor => read_editor_string()?,
138139
DatalogInput::FromStdin => read_stdin_string("datalog program")?,
@@ -153,22 +154,22 @@ pub fn read_authority_from(
153154
}
154155
}
155156

156-
builder
157-
.add_code_with_params(&string, params, scope_params)
157+
let mut builder = builder
158+
.code_with_params(&string, params, scope_params)
158159
.map_err(|e| ParseError("datalog statements".to_string(), e.to_string()))?;
159160
if let Some(ctx) = context {
160-
builder.set_context(ctx.to_owned());
161+
builder = builder.context(ctx.to_owned());
161162
}
162163

163-
Ok(())
164+
Ok(builder)
164165
}
165166

166167
pub fn read_block_from(
167168
from: &DatalogInput,
168169
all_params: &[Param],
169170
context: &Option<String>,
170-
builder: &mut BlockBuilder,
171-
) -> Result<()> {
171+
builder: BlockBuilder,
172+
) -> Result<BlockBuilder> {
172173
let string = match from {
173174
DatalogInput::FromEditor => read_editor_string()?,
174175
DatalogInput::FromStdin => read_stdin_string("datalog program")?,
@@ -188,22 +189,22 @@ pub fn read_block_from(
188189
}
189190
}
190191
}
191-
builder
192-
.add_code_with_params(&string, params, scope_params)
192+
let mut builder = builder
193+
.code_with_params(&string, params, scope_params)
193194
.map_err(|e| ParseError("datalog statements".to_string(), e.to_string()))?;
194195

195196
if let Some(ctx) = context {
196-
builder.set_context(ctx.to_owned());
197+
builder = builder.context(ctx.to_owned());
197198
}
198199

199-
Ok(())
200+
Ok(builder)
200201
}
201202

202203
pub fn read_authorizer_from(
203204
from: &DatalogInput,
204205
all_params: &[Param],
205-
authorizer: &mut Authorizer,
206-
) -> Result<()> {
206+
builder: AuthorizerBuilder,
207+
) -> Result<AuthorizerBuilder> {
207208
let string = match from {
208209
DatalogInput::FromEditor => read_editor_string()?,
209210
DatalogInput::FromStdin => read_stdin_string("datalog program")?,
@@ -223,14 +224,14 @@ pub fn read_authorizer_from(
223224
}
224225
}
225226
}
226-
authorizer
227-
.add_code_with_params(&string, params, scope_params)
227+
let builder = builder
228+
.code_with_params(&string, params, scope_params)
228229
.map_err(|e| ParseError("datalog statements".to_string(), e.to_string()))?;
229230

230-
Ok(())
231+
Ok(builder)
231232
}
232233

233-
pub fn read_private_key_from(from: &KeyBytes) -> Result<PrivateKey> {
234+
pub fn read_private_key_from(from: &KeyBytes, alg: Algorithm) -> Result<PrivateKey> {
234235
let bytes = match from {
235236
KeyBytes::FromStdin(KeyFormat::RawBytes) => read_stdin_bytes()?,
236237
KeyBytes::FromStdin(KeyFormat::HexKey) => {
@@ -246,11 +247,11 @@ pub fn read_private_key_from(from: &KeyBytes) -> Result<PrivateKey> {
246247
)?,
247248
KeyBytes::HexString(str) => hex::decode(str)?,
248249
};
249-
PrivateKey::from_bytes(&bytes)
250+
PrivateKey::from_bytes(&bytes, alg)
250251
.map_err(|e| ParseError("private key".to_string(), format!("{}", &e)).into())
251252
}
252253

253-
pub fn read_public_key_from(from: &KeyBytes) -> Result<PublicKey> {
254+
pub fn read_public_key_from(from: &KeyBytes, alg: Algorithm) -> Result<PublicKey> {
254255
let bytes = match from {
255256
KeyBytes::FromStdin(KeyFormat::RawBytes) => read_stdin_bytes()?,
256257
KeyBytes::FromStdin(KeyFormat::HexKey) => {
@@ -266,7 +267,7 @@ pub fn read_public_key_from(from: &KeyBytes) -> Result<PublicKey> {
266267
)?,
267268
KeyBytes::HexString(str) => hex::decode(str)?,
268269
};
269-
PublicKey::from_bytes(&bytes)
270+
PublicKey::from_bytes(&bytes, alg)
270271
.map_err(|e| ParseError("public key".to_string(), format!("{}", &e)).into())
271272
}
272273

@@ -424,7 +425,7 @@ pub fn parse_param(kv: &str) -> Result<Param, std::io::Error> {
424425
))?;
425426
let bytes =
426427
hex::decode(hex_key).map_err(|e| Error::new(ErrorKind::Other, format!("{}", &e)));
427-
let pubkey = PublicKey::from_bytes(&bytes?)
428+
let pubkey = PublicKey::from_bytes(&bytes?, Algorithm::Ed25519)
428429
.map_err(|e| Error::new(ErrorKind::Other, format!("{}", &e)))?;
429430
Ok(Param::PublicKey(name.to_string(), pubkey))
430431
},

0 commit comments

Comments
 (0)