use clap::Parser;
use std::path::PathBuf;
use crate::input::*;
#[derive(Parser)]
#[clap(version, author, verbatim_doc_comment)]
pub struct Opts {
#[clap(subcommand)]
pub subcmd: SubCommand,
}
#[derive(Parser)]
pub enum SubCommand {
#[clap(name = "keypair")]
KeyPairCmd(KeyPairCmd),
Inspect(Box<Inspect>),
InspectSnapshot(Box<InspectSnapshot>),
Generate(Generate),
Attenuate(Attenuate),
GenerateThirdPartyBlockRequest(GenerateThirdPartyBlockRequest),
GenerateThirdPartyBlock(GenerateThirdPartyBlock),
AppendThirdPartyBlock(AppendThirdPartyBlock),
Seal(Seal),
}
#[derive(Parser)]
#[clap(display_order(0))]
pub struct KeyPairCmd {
#[clap(long, value_name("PRIVATE_KEY"), conflicts_with("from-file"))]
pub from_private_key: Option<String>,
#[clap(long, value_name("PRIVATE_KEY_FILE"))]
pub from_file: Option<PathBuf>,
#[clap(
long,
value_enum,
default_value_t,
value_name("PRIVATE_KEY_FORMAT"),
requires("from-private-key"),
requires("from-file")
)]
pub from_format: KeyFormat,
#[clap(
long,
value_enum,
value_name("PRIVATE_KEY_ALGORITHM"),
requires("from-file")
)]
pub from_algorithm: Option<Algorithm>,
#[clap(
long,
value_enum,
default_value_t,
value_name("KEYPAIR_ALGORITHM"),
conflicts_with("from-private-key"),
conflicts_with("from-file")
)]
pub key_algorithm: Algorithm,
#[clap(long, value_enum, default_value_t)]
pub key_output_format: KeyFormat,
#[clap(long, conflicts_with("only-private-key"))]
pub only_public_key: bool,
#[clap(long, conflicts_with("only-public-key"))]
pub only_private_key: bool,
}
#[derive(Parser)]
#[clap(display_order(1))]
pub struct Generate {
#[clap(parse(from_os_str), value_name("DATALOG_FILE"))]
pub authority_file: Option<PathBuf>,
#[clap(long)]
pub root_key_id: Option<u32>,
#[clap(flatten)]
pub param_arg: common_args::ParamArg,
#[clap(long)]
pub raw: bool,
#[clap(flatten)]
pub private_key_args: common_args::PrivateKeyArgs,
#[clap(long)]
pub context: Option<String>,
#[clap(
long,
parse(try_from_str = parse_ttl),
value_name("TTL"),
verbatim_doc_comment
)]
pub add_ttl: Option<Ttl>,
}
#[derive(Parser)]
#[clap(display_order(2))]
pub struct Attenuate {
#[clap(flatten)]
pub biscuit_input_args: common_args::BiscuitInputArgs,
#[clap(long)]
pub raw_output: bool,
#[clap(flatten)]
pub block_args: common_args::BlockArgs,
#[clap(flatten)]
pub param_arg: common_args::ParamArg,
}
#[derive(Parser)]
#[clap(display_order(3))]
pub struct Inspect {
#[clap(long)]
pub json: bool,
#[clap(flatten)]
pub biscuit_input_args: common_args::BiscuitInputArgs,
#[clap(long, conflicts_with("public-key-file"))]
pub public_key: Option<String>,
#[clap(long, conflicts_with("public-key"), parse(from_os_str))]
pub public_key_file: Option<PathBuf>,
#[clap(long, value_enum, default_value_t)]
pub public_key_format: KeyFormat,
#[clap(long, value_enum, requires("public-key-file"))]
pub public_key_algorithm: Option<Algorithm>,
#[clap(flatten)]
pub run_limits_args: common_args::RunLimitArgs,
#[clap(flatten)]
pub authorization_args: common_args::AuthorizeArgs,
#[clap(flatten)]
pub query_args: common_args::QueryArgs,
#[clap(flatten)]
pub param_arg: common_args::ParamArg,
#[clap(long, parse(from_os_str), value_name("SNAPSHOT_FILE"))]
pub dump_snapshot_to: Option<PathBuf>,
#[clap(long, requires("dump-snapshot-to"))]
pub dump_raw_snapshot: bool,
#[clap(long, parse(from_os_str), value_name("SNAPSHOT_FILE"))]
pub dump_policies_snapshot_to: Option<PathBuf>,
#[clap(long, requires("dump-snapshot-to"))]
pub dump_raw_policies_snapshot: bool,
}
#[derive(Parser)]
#[clap(display_order(4))]
pub struct InspectSnapshot {
#[clap(long)]
pub json: bool,
#[clap(parse(from_os_str))]
pub snapshot_file: PathBuf,
#[clap(long)]
pub raw_input: bool,
#[clap(flatten)]
pub run_limits_args: common_args::RunLimitArgs,
#[clap(flatten)]
pub query_args: common_args::QueryArgs,
#[clap(flatten)]
pub param_arg: common_args::ParamArg,
}
#[derive(Parser)]
#[clap(display_order(5))]
pub struct GenerateThirdPartyBlockRequest {
#[clap(flatten)]
pub biscuit_input_args: common_args::BiscuitInputArgs,
#[clap(long)]
pub raw_output: bool,
}
#[derive(Parser)]
#[clap(display_order(6))]
pub struct GenerateThirdPartyBlock {
#[clap(parse(from_os_str))]
pub request_file: PathBuf,
#[clap(long)]
pub raw_input: bool,
#[clap(flatten)]
pub private_key_args: common_args::PrivateKeyArgs,
#[clap(long)]
pub raw_output: bool,
#[clap(flatten)]
pub block_args: common_args::BlockArgs,
#[clap(flatten)]
pub param_arg: common_args::ParamArg,
}
#[derive(Parser)]
#[clap(display_order(7))]
pub struct AppendThirdPartyBlock {
#[clap(flatten)]
pub biscuit_input_args: common_args::BiscuitInputArgs,
#[clap(long)]
pub raw_output: bool,
#[clap(long)]
pub block_contents: Option<String>,
#[clap(
long,
parse(from_os_str),
conflicts_with("block-contents"),
required_unless_present("block-contents")
)]
pub block_contents_file: Option<PathBuf>,
#[clap(long, requires("block-contents-file"))]
pub raw_block_contents: bool,
}
#[derive(Parser)]
#[clap(display_order(8))]
pub struct Seal {
#[clap(flatten)]
pub biscuit_input_args: common_args::BiscuitInputArgs,
#[clap(long)]
pub raw_output: bool,
}
mod common_args {
use crate::input::*;
use biscuit_auth::builder::Rule;
use chrono::Duration;
use clap::Parser;
use std::path::PathBuf;
#[derive(Parser)]
pub struct QueryArgs {
#[clap(
long,
value_parser = clap::builder::ValueParser::new(parse_rule),
value_name("DATALOG_RULE")
)]
pub query: Option<Rule>,
#[clap(long, requires("query"))]
pub query_all: bool,
}
#[derive(Parser)]
pub struct ParamArg {
#[clap(
long,
value_parser = clap::builder::ValueParser::new(parse_param),
verbatim_doc_comment,
value_name = "key[:type]=value",
)]
pub param: Vec<Param>,
}
#[derive(Parser)]
pub struct RunLimitArgs {
#[clap(long)]
pub max_facts: Option<u64>,
#[clap(long)]
pub max_iterations: Option<u64>,
#[clap(
long,
parse(try_from_str = parse_duration),
value_name("DURATION"),
verbatim_doc_comment
)]
pub max_time: Option<Duration>,
}
#[derive(Parser)]
pub struct AuthorizeArgs {
#[clap(
long,
alias("verify-interactive"),
conflicts_with("authorize-with"),
conflicts_with("authorize-with-file"),
conflicts_with("authorize-with-snapshot"),
conflicts_with("authorize-with-snapshot-file")
)]
pub authorize_interactive: bool,
#[clap(
long,
parse(from_os_str),
alias("verify-with-file"),
conflicts_with("authorize-with"),
conflicts_with("authorize-with-snapshot"),
conflicts_with("authorize-with-snapshot-file"),
conflicts_with("authorize-interactive"),
value_name("DATALOG_FILE")
)]
pub authorize_with_file: Option<PathBuf>,
#[clap(
long,
alias("verify-with"),
conflicts_with("authorize-with-file"),
conflicts_with("authorize-with-snapshot"),
conflicts_with("authorize-with-snapshot-file"),
conflicts_with("authorize-interactive"),
value_name("DATALOG")
)]
pub authorize_with: Option<String>,
#[clap(
long,
conflicts_with("authorize-with"),
conflicts_with("authorize-with-file"),
conflicts_with("authorize-with-snapshot-file"),
conflicts_with("authorize-interactive"),
value_name("SNAPSHOT")
)]
pub authorize_with_snapshot: Option<String>,
#[clap(
long,
conflicts_with("authorize-with"),
conflicts_with("authorize-with-file"),
conflicts_with("authorize-with-snapshot"),
conflicts_with("authorize-interactive"),
value_name("SNAPSHOT_FILE")
)]
pub authorize_with_snapshot_file: Option<PathBuf>,
#[clap(long, requires("authorize-with-snapshot-file"))]
pub authorize_with_raw_snapshot_file: bool,
#[clap(long)]
pub include_time: bool,
}
#[derive(Parser)]
pub struct BlockArgs {
#[clap(long, value_name("DATALOG"))]
pub block: Option<String>,
#[clap(
long,
parse(from_os_str),
conflicts_with = "block",
value_name("DATALOG_FILE")
)]
pub block_file: Option<PathBuf>,
#[clap(long)]
pub context: Option<String>,
#[clap(
long,
parse(try_from_str = parse_ttl),
value_name("TTL"),
verbatim_doc_comment
)]
pub add_ttl: Option<Ttl>,
}
#[derive(Parser)]
pub struct BiscuitInputArgs {
#[clap(parse(from_os_str))]
pub biscuit_file: PathBuf,
#[clap(long)]
pub raw_input: bool,
}
#[derive(Parser)]
pub struct PrivateKeyArgs {
#[clap(long, required_unless_present("private-key-file"))]
pub private_key: Option<String>,
#[clap(
long,
parse(from_os_str),
required_unless_present("private-key"),
conflicts_with = "private-key"
)]
pub private_key_file: Option<PathBuf>,
#[clap(long, value_enum, default_value_t)]
pub private_key_format: KeyFormat,
#[clap(
long,
value_enum,
value_name("PRIVATE_KEY_ALGORITHM"),
requires("private-key-file")
)]
pub private_key_algorithm: Option<Algorithm>,
}
}
#[test]
fn verify_cli() {
use clap::CommandFactory;
Opts::command().debug_assert();
}