Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use normal CLI with parameters and options #53

Merged
merged 8 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1136,12 +1136,13 @@ You can find various usage examples in the [examples folder](https://github.com/

### Setup

To run the examples, set your `Project ID` by setting the `DESCOPE_PROJECT_ID` env var or directly
in the sample code.
To run the examples, set your `Project ID` and `Management Key` by setting the `DESCOPE_PROJECT_ID` and `DESCOPE_MANAGEMENT_KEY` env vars or directly in the sample code.
Find your Project ID in the [Descope console](https://app.descope.com/settings/project).
Find your management key in the [Descope console](https://app.descope.com/settings/company/managementkeys).

```bash
export DESCOPE_PROJECT_ID=<ProjectID>
export DESCOPE_MANAGEMENT_KEY=<ManagementKey>
```

Alternatively, you can create a `.env` file in the working folder with your project ID and management key.
Expand All @@ -1152,17 +1153,26 @@ DESCOPE_MANAGEMENT_KEY=<ManagementKey>

### Run an example

1. Run this command in your project to build the examples.
1. Make sure that the main Descope java-sdk is installed in the local Maven. Run this in the main folder.
```bash
mvn install
```
2. Run this command in your project to build the example in the `examples/management-cli` folder.

```bash
mvn package
```

2. Run a specific example
3. Run a specific example

```bash
# CLI example
java -jar target/management-cli.jar command-name -option1 -option2
java -jar target/management-cli-1.0.jar command-name -option1 -option2
# For example to display 10 users:
java -jar target/management-cli-1.0.jar user-search-all -l 10
# Help is available on all commands and within the command itself:
java -jar target/management-cli-1.0.jar -h
java -jar target/management-cli-1.0.jar user-search-all -h
```

### Using Visual Studio Code
Expand Down
25 changes: 19 additions & 6 deletions examples/management-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.descope</groupId>
<artifactId>management-cli</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand All @@ -22,9 +22,24 @@
<version>1.0</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.5.0</version>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.7.4</version>
</dependency>
<dependency>
<artifactId>jackson-databind</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
<version>2.15.2</version>
</dependency>
<dependency>
<artifactId>jackson-datatype-jsr310</artifactId>
<groupId>com.fasterxml.jackson.datatype</groupId>
<version>2.15.2</version>
</dependency>
<dependency>
<artifactId>commons-lang3</artifactId>
<groupId>org.apache.commons</groupId>
<version>3.12.0</version>
</dependency>

<!-- LOMBOK -->
Expand Down Expand Up @@ -70,6 +85,4 @@
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.descope;

import picocli.CommandLine.Option;

public abstract class AccessKeyBase extends HelpBase {
@Option(names = { "-i", "--keyid" }, required = true, description = "the key ID")
String keyId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.descope;

import com.descope.client.DescopeClient;
import com.descope.exception.DescopeException;
import com.descope.model.auth.AssociatedTenant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(name = "access-key-create", description = "Create a Descope access key")
public class AccessKeyCreate extends HelpBase implements Callable<Integer> {

@Option(names = { "-n", "--name"}, description = "Access key name")
String name;
@Option(names = { "-e", "--expiration"}, description = "Number of days this key is valid")
int expiration;
@Option(names = { "-r", "--role"}, description = "Optional role for the access key. Multiple can be provided.")
List<String> roles;
@Option(names = { "-t", "--tenant"}, description = "Optional tenant-roles pairs. Each is tenantId:role1,role2,role3")
List<String> tenants;

@Override
public Integer call() {
int exitCode = 0;
try {
var client = new DescopeClient();
var keyService = client.getManagementServices().getAccessKeyService();
var associatedTenants = new ArrayList<AssociatedTenant>();
if (tenants != null) {
for (var t : tenants) {
String[] parts = t.split(":");
if (parts.length == 2) {
String[] roles = parts[1].split(",");
if (roles.length > 0) {
associatedTenants.add(new AssociatedTenant(parts[0], Arrays.asList(roles)));
}
}
}
}
var res = keyService.create(name, expiration, roles, associatedTenants);
System.out.printf("Key %s created with id %s\n", name, res.getKey().getId());
} catch (DescopeException de) {
exitCode = 1;
System.err.println(String.format("%s - %s", de.getCode(), de.getMessage()));
}
return exitCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.descope;

import com.descope.client.DescopeClient;
import com.descope.exception.DescopeException;
import java.util.concurrent.Callable;
import picocli.CommandLine.Command;

@Command(name = "access-key-delete", description = "Delete a Descope access key")
public class AccessKeyDelete extends AccessKeyBase implements Callable<Integer> {

@Override
public Integer call() {
int exitCode = 0;
try {
var client = new DescopeClient();
var keyService = client.getManagementServices().getAccessKeyService();
keyService.delete(keyId);
System.out.printf("Key %s deleted\n", keyId);
} catch (DescopeException de) {
exitCode = 1;
System.err.println(String.format("%s - %s", de.getCode(), de.getMessage()));
}
return exitCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.descope;

import com.descope.client.DescopeClient;
import com.descope.exception.DescopeException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.concurrent.Callable;
import picocli.CommandLine.Command;

@Command(name = "access-key-load", description = "Load Descope access key details")
public class AccessKeyLoad extends AccessKeyBase implements Callable<Integer> {

@Override
public Integer call() throws JsonProcessingException {
int exitCode = 0;
try {
var client = new DescopeClient();
var keyService = client.getManagementServices().getAccessKeyService();
var res = keyService.load(keyId);
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(res));
} catch (DescopeException de) {
exitCode = 1;
System.err.println(String.format("%s - %s", de.getCode(), de.getMessage()));
}
return exitCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.descope;

import com.descope.client.DescopeClient;
import com.descope.exception.DescopeException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.concurrent.Callable;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(name = "access-key-search-all", description = "Search Descope access keys")
public class AccessKeySearch extends HelpBase implements Callable<Integer> {

@Option(names = { "-t", "--tenant"}, description = "Optional tenant ids. Multiple tenants supported.")
List<String> tenants;

@Override
public Integer call() throws JsonProcessingException {
int exitCode = 0;
try {
var client = new DescopeClient();
var keyService = client.getManagementServices().getAccessKeyService();
var res = keyService.searchAll(tenants);
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(res));
} catch (DescopeException de) {
exitCode = 1;
System.err.println(String.format("%s - %s", de.getCode(), de.getMessage()));
}
return exitCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.descope;

import com.descope.client.DescopeClient;
import com.descope.exception.DescopeException;
import java.util.concurrent.Callable;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(name = "access-key-update", description = "Update a Descope access key")
public class AccessKeyUpdate extends AccessKeyBase implements Callable<Integer> {

@Option(names = { "-n", "--name"}, description = "Access key name")
String name;

@Override
public Integer call() {
int exitCode = 0;
try {
var client = new DescopeClient();
var keyService = client.getManagementServices().getAccessKeyService();
keyService.update(keyId, name);
System.out.printf("Key %s updated\n", name);
} catch (DescopeException de) {
exitCode = 1;
System.err.println(String.format("%s - %s", de.getCode(), de.getMessage()));
}
return exitCode;
}
}
55 changes: 55 additions & 0 deletions examples/management-cli/src/main/java/com/descope/AuditSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.descope;

import com.descope.client.DescopeClient;
import com.descope.exception.DescopeException;
import com.descope.model.audit.AuditSearchRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.Callable;
import org.apache.commons.lang3.StringUtils;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Command(name = "audit-search", description = "Search audit trail")
public class AuditSearch extends HelpBase implements Callable<Integer> {

@Option(names = { "-f", "--from"}, description = "Search last number of days")
int from;

@Option(names = { "-t", "--to"}, description = "Search up to number of days")
int to;

@Option(names = { "-x", "--text"}, description = "Search for text in relevant fields")
String text;


@Override
public Integer call() throws JsonProcessingException {
int exitCode = 0;
try {
var builder = AuditSearchRequest.builder();
if (from != 0) {
builder = builder.from(Instant.now().minus(Duration.ofDays(from)));
}
if (to != 0) {
builder = builder.to(Instant.now().minus(Duration.ofDays(from)));
}
if (StringUtils.isNotBlank(text)) {
builder = builder.text(text);
}
var client = new DescopeClient();
var auditService = client.getManagementServices().getAuditService();
var res = auditService.search(builder.build());
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(res));
} catch (DescopeException de) {
exitCode = 1;
System.err.println(String.format("%s - %s", de.getCode(), de.getMessage()));
}
return exitCode;
}
}
Loading