Skip to content

Commit

Permalink
feat: implement auth command in xlinectl
Browse files Browse the repository at this point in the history
Signed-off-by: bsbds <69835502+bsbds@users.noreply.github.com>
  • Loading branch information
bsbds committed Sep 20, 2023
1 parent f76bf05 commit c9e6ddf
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 4 deletions.
62 changes: 62 additions & 0 deletions xlinectl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,68 @@ snapshot saved to: /tmp/foo.snapshot
## Authentication commands

### AUTH
Manage authentication

### AUTH ENABLE
Enable authentication

#### Usage

```bash
auth enable
```

#### Output

```
Authentication enabled
```

#### Examples
```bash
./xlinectl user add root rootpw
./xlienctl user grant-role root root
# Enable authentication
./xlinectl auth enable
Authentication enabled
```

### AUTH DISABLE
Disable authentication

#### Usage

```bash
auth disable
```

#### Output

```
Authentication disabled
```

#### Examples
```bash
# Disable authentication
./xlinectl --user root:root auth disable
Authentication disabled
```

### AUTH STATUS
Status of authentication

#### Usage

```bash
auth status
```

#### Examples
```bash
# Check the status of authentication
./xlinectl auth status
```

### ROLE

Expand Down
17 changes: 17 additions & 0 deletions xlinectl/src/command/auth/disable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use clap::{ArgMatches, Command};
use xline_client::{error::Result, Client};

use crate::utils::printer::Printer;

/// Definition of `disable` command
pub(super) fn command() -> Command {
Command::new("disable").about("disable authentication")
}

/// Execute the command
pub(super) async fn execute(client: &mut Client, _matches: &ArgMatches) -> Result<()> {
let resp = client.auth_client().auth_disable().await?;
resp.print();

Ok(())
}
17 changes: 17 additions & 0 deletions xlinectl/src/command/auth/enable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use clap::{ArgMatches, Command};
use xline_client::{error::Result, Client};

use crate::utils::printer::Printer;

/// Definition of `enable` command
pub(super) fn command() -> Command {
Command::new("enable").about("Enable authentication")
}

/// Execute the command
pub(super) async fn execute(client: &mut Client, _matches: &ArgMatches) -> Result<()> {
let resp = client.auth_client().auth_enable().await?;
resp.print();

Ok(())
}
27 changes: 27 additions & 0 deletions xlinectl/src/command/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use clap::{ArgMatches, Command};
use xline_client::{error::Result, Client};

use crate::handle_matches;

/// Auth disable command
mod disable;
/// Auth enable command
mod enable;
/// Auth status command
mod status;

/// Definition of `auth` command
pub(crate) fn command() -> Command {
Command::new("auth")
.about("Manage authentication")
.subcommand(enable::command())
.subcommand(disable::command())
.subcommand(status::command())
}

/// Execute the command
pub(crate) async fn execute(mut client: &mut Client, matches: &ArgMatches) -> Result<()> {
handle_matches!(matches, client, {enable, disable, status});

Ok(())
}
17 changes: 17 additions & 0 deletions xlinectl/src/command/auth/status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use clap::{ArgMatches, Command};
use xline_client::{error::Result, Client};

use crate::utils::printer::Printer;

/// Definition of `status` command
pub(super) fn command() -> Command {
Command::new("status").about("Status of authentication")
}

/// Execute the command
pub(super) async fn execute(client: &mut Client, _matches: &ArgMatches) -> Result<()> {
let resp = client.auth_client().auth_status().await?;
resp.print();

Ok(())
}
2 changes: 2 additions & 0 deletions xlinectl/src/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// Auth command
pub(crate) mod auth;
/// Delete command
pub(crate) mod delete;
/// Get command
Expand Down
5 changes: 3 additions & 2 deletions xlinectl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ use ext_utils::config::ClientConfig;
use xline_client::{Client, ClientOptions};

use crate::{
command::{delete, get, lease, put, snapshot},
command::{auth, delete, get, lease, put, snapshot},
utils::{
parser::parse_user,
printer::{set_printer_type, PrinterType},
Expand Down Expand Up @@ -230,6 +230,7 @@ fn cli() -> Command {
.subcommand(delete::command())
.subcommand(lease::command())
.subcommand(snapshot::command())
.subcommand(auth::command())
}

#[tokio::main]
Expand All @@ -256,6 +257,6 @@ async fn main() -> Result<()> {
set_printer_type(printer_type);

let mut client = Client::connect(endpoints, options).await?;
handle_matches!(matches, client, { get, put, delete, lease, snapshot });
handle_matches!(matches, client, { get, put, delete, lease, snapshot, auth });
Ok(())
}
44 changes: 42 additions & 2 deletions xlinectl/src/utils/printer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::sync::OnceLock;

use xlineapi::{
DeleteRangeResponse, KeyValue, LeaseGrantResponse, LeaseKeepAliveResponse, LeaseLeasesResponse,
LeaseRevokeResponse, LeaseTimeToLiveResponse, PutResponse, RangeResponse, ResponseHeader,
AuthDisableResponse, AuthEnableResponse, AuthStatusResponse, DeleteRangeResponse, KeyValue,
LeaseGrantResponse, LeaseKeepAliveResponse, LeaseLeasesResponse, LeaseRevokeResponse,
LeaseTimeToLiveResponse, PutResponse, RangeResponse, ResponseHeader,
};

/// The global printer type config
Expand Down Expand Up @@ -155,6 +156,45 @@ impl Printer for LeaseTimeToLiveResponse {
}
}

impl Printer for AuthEnableResponse {
fn simple(&self) {
println!("Authentication enabled");
}

fn field(&self) {
FieldPrinter::header(self.header.as_ref());
println!("Authentication enabled");
}
}

impl Printer for AuthDisableResponse {
fn simple(&self) {
println!("Authentication disabled");
}

fn field(&self) {
FieldPrinter::header(self.header.as_ref());
println!("Authentication disabled");
}
}

impl Printer for AuthStatusResponse {
fn simple(&self) {
println!(
"enabled: {}, revision: {}",
self.enabled, self.auth_revision
);
}

fn field(&self) {
FieldPrinter::header(self.header.as_ref());
println!(
"enabled: {}, revision: {}",
self.enabled, self.auth_revision
);
}
}

/// Simple Printer of common response types
struct SimplePrinter;

Expand Down

0 comments on commit c9e6ddf

Please sign in to comment.