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

Java wrapper for SDK #179

Merged
merged 24 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
82eefed
Java wrapper for SDK
lukpotSym Aug 21, 2023
e553d5f
Merge branch 'master' into feature/java-wrapper
lukpotSym Aug 22, 2023
36d6620
split projects and secrets from the main client
lukpotSym Aug 22, 2023
5043c7b
Merge branch 'bitwarden:master' into feature/java-wrapper
lukpotSym Sep 12, 2023
da72b7b
switch to gradle
lukpotSym Sep 19, 2023
9cf23bd
Update .github/workflows/generate_schemas.yml
lukpotSym Sep 29, 2023
ab5908c
Merge branch 'master' into feature/java-wrapper
lukpotSym Sep 29, 2023
f00e7ec
added build/publish workflows
lukpotSym Oct 2, 2023
52c5df5
switch to gradle-build-action
lukpotSym Oct 2, 2023
f71fd69
cleanup
lukpotSym Oct 4, 2023
251ef47
build.gradle updates
lukpotSym Oct 4, 2023
bb2af7f
unwrap sdk responses, addressing PR comments
lukpotSym Oct 18, 2023
243d2a2
Merge branch 'master' into feature/java-wrapper
lukpotSym Oct 18, 2023
9b11383
Apply suggestions from code review
lukpotSym Nov 23, 2023
fd3d420
Merge branch 'master' into feature/java-wrapper
lukpotSym Nov 23, 2023
b8085ca
spacing changes and tasks.json fix
lukpotSym Nov 25, 2023
75785c9
Merge branch 'master' into feature/java-wrapper
lukpotSym Nov 25, 2023
299bd8d
Update languages/java/README.md
lukpotSym Nov 27, 2023
055a429
Merge branch 'master' into feature/java-wrapper
lukpotSym Nov 27, 2023
b471480
fix path for generate_schemas workflow and run prettier on schems
lukpotSym Nov 27, 2023
c57ef08
Merge branch 'master' into feature/java-wrapper
lukpotSym Nov 27, 2023
65e25d4
fix duplicate lib files gradle issue and rename build task
lukpotSym Nov 27, 2023
7df172c
commented out jar task to unblock github workflow
lukpotSym Nov 27, 2023
40f07a5
Merge branch 'master' into feature/java-wrapper
lukpotSym Nov 27, 2023
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
11 changes: 9 additions & 2 deletions .github/workflows/generate_schemas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
toolchain: stable

- name: Set up Node
uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d # v3.8.1
uses: actions/setup-node@e33196f7422957bea03ed53f6fbb155025ffc7b8 # v3.7.0
with:
cache: "npm"
cache-dependency-path: "package-lock.json"
Expand All @@ -31,7 +31,7 @@ jobs:
run: npm ci

- name: Cache cargo registry
uses: Swatinem/rust-cache@e207df5d269b42b69c8bc5101da26f7d31feddb4 # v2.6.2
uses: Swatinem/rust-cache@b8a6852b4f997182bdea832df3f9e153038b5191 # v2.6.0

- name: NPM Schemas
run: npm run schemas
Expand Down Expand Up @@ -63,3 +63,10 @@ jobs:
name: sdk-schemas-json
path: ${{ github.workspace }}/support/schemas/*
if-no-files-found: error

- name: Upload java schemas artifact
uses: actions/upload-artifact@ #TODO
lukpotSym marked this conversation as resolved.
Show resolved Hide resolved
with:
name: sdk-schemas-java
path: ${{ github.workspace }}/languages/java/src/main/java/bit/sdk/schema/*
if-no-files-found: error
lukpotSym marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ crates/bitwarden-napi/src-ts/bitwarden_client/schemas.ts
languages/csharp/schemas.cs
languages/js_webassembly/bitwarden_client/schemas.ts
languages/python/BitwardenClient/schemas.py
languages/java/src/main/java/bit/sdk/schema
13 changes: 12 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@
"options": {
"cwd": "${workspaceFolder}/languages/python"
}
}
},
{
"label": "buildJava",
"type": "shell",
"command": "mvn",
"args": [
"clean",
"install"
],
"dependsOrder": "sequence",
"dependsOn": ["rust: bitwarden-c build"]
}
lukpotSym marked this conversation as resolved.
Show resolved Hide resolved
lukpotSym marked this conversation as resolved.
Show resolved Hide resolved
]
}
2 changes: 1 addition & 1 deletion crates/sdk-schemas/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn main() -> Result<()> {
bitwarden::secrets_manager::secrets::SecretResponse,
bitwarden::secrets_manager::secrets::SecretsDeleteResponse,
bitwarden::secrets_manager::projects::ProjectResponse,
bitwarden::secrets_manager::projects::ProjectResponse,
bitwarden::secrets_manager::projects::ProjectsResponse,
lukpotSym marked this conversation as resolved.
Show resolved Hide resolved
bitwarden::secrets_manager::projects::ProjectsDeleteResponse,
};

Expand Down
2 changes: 2 additions & 0 deletions languages/java/.gitignore
lukpotSym marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
.idea
138 changes: 138 additions & 0 deletions languages/java/pom.xml
lukpotSym marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>bit.sdk</groupId>
<artifactId>JavaWrapper</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.10</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.10</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.12.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>../../target/debug</directory>
<includes>
<include>libbitwarden_c*.dylib</include>
<include>libbitwarden_c*.so</include>
<include>bitwarden_c*.dll</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<!-- <profiles>-->
<!-- <profile>-->
<!-- <id>debug</id>-->
<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <artifactId>maven-resources-plugin</artifactId>-->
<!-- <version>3.3.1</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>copy-resources</id>-->
<!-- <phase>validate</phase>-->
<!-- <goals>-->
<!-- <goal>copy-resources</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <outputDirectory>${project.build.outputDirectory}-->
<!-- </outputDirectory>-->
<!-- <resources>-->
<!-- <resource>-->
<!-- <directory>../../target/debug</directory>-->
<!-- <includes>-->
<!-- <include>libbitwarden_c*.dylib</include>-->
<!-- <include>libbitwarden_c*.so</include>-->
<!-- <include>bitwarden_c*.dll</include>-->
<!-- </includes>-->
<!-- </resource>-->
<!-- </resources>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
<!-- </profile>-->

<!-- <profile>-->
<!-- <id>release</id>-->
<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <artifactId>maven-resources-plugin</artifactId>-->
<!-- <version>3.3.1</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>copy-resources</id>-->
<!-- <phase>validate</phase>-->
<!-- <goals>-->
<!-- <goal>copy-resources</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <outputDirectory>${project.build.outputDirectory}-->
<!-- </outputDirectory>-->
<!-- <resources>-->
<!-- <resource>-->
<!-- <directory>../../target/release/</directory>-->
<!-- <includes>-->
<!-- <include>libbitwarden_c*.dylib</include>-->
<!-- <include>libbitwarden_c*.so</include>-->
<!-- <include>bitwarden_c*.dll</include>-->
<!-- </includes>-->
<!-- </resource>-->
<!-- </resources>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
<!-- </profile>-->
<!-- </profiles>-->

</project>
72 changes: 72 additions & 0 deletions languages/java/src/main/java/bit/sdk/BitwardenClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package bit.sdk;
lukpotSym marked this conversation as resolved.
Show resolved Hide resolved

import bit.sdk.schema.AccessTokenLoginRequest;
import bit.sdk.schema.ClientSettings;
import bit.sdk.schema.Command;
import bit.sdk.schema.Converter;
import bit.sdk.schema.ResponseForAPIKeyLoginResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import java.util.function.Function;

public class BitwardenClient implements AutoCloseable {

static <T, R> Function<T, R> throwingFunctionWrapper(ThrowingFunction<T, R, Exception> throwingFunction) {

return i -> {
try {
return throwingFunction.accept(i);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
};
}

private Pointer client;

private BitwardenLibrary library;

private CommandRunner commandRunner;

private boolean isClientOpen;

private Projects projects;

private Secrets secrets;

public BitwardenClient(ClientSettings clientSettings) throws JsonProcessingException {
System.setProperty("jna.library.path", System.getProperty("user.dir") + "/libs");
lukpotSym marked this conversation as resolved.
Show resolved Hide resolved
library = Native.load("bitwarden_c", BitwardenLibrary.class);
client = library.init(Converter.ClientSettingsToJsonString(clientSettings));
commandRunner = new CommandRunner(library, client);
projects = new Projects(commandRunner);
secrets = new Secrets(commandRunner);
isClientOpen = true;
}

public ResponseForAPIKeyLoginResponse accessTokenLogin(String accessToken) {
lukpotSym marked this conversation as resolved.
Show resolved Hide resolved
Command command = new Command();
AccessTokenLoginRequest accessTokenLoginRequest = new AccessTokenLoginRequest();
accessTokenLoginRequest.setAccessToken(accessToken);
command.setAccessTokenLogin(accessTokenLoginRequest);
return commandRunner.runCommand(command,
throwingFunctionWrapper(Converter::ResponseForAPIKeyLoginResponseFromJsonString));
}

public Projects projects() {
return projects;
}

public Secrets secrets() {
return secrets;
}

@Override
public void close() {
if (isClientOpen) {
library.free_mem(client);
isClientOpen = false;
}
}
}
13 changes: 13 additions & 0 deletions languages/java/src/main/java/bit/sdk/BitwardenLibrary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package bit.sdk;

import com.sun.jna.Library;
import com.sun.jna.Pointer;

public interface BitwardenLibrary extends Library {

Pointer init(String clientSettings);

void free_mem(Pointer client);

String run_command(String command, Pointer client);
}
42 changes: 42 additions & 0 deletions languages/java/src/main/java/bit/sdk/CommandRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package bit.sdk;

import bit.sdk.schema.Command;
import bit.sdk.schema.Converter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.jna.Pointer;
import java.io.IOException;
import java.util.function.Function;

class CommandRunner {

private BitwardenLibrary library;

private Pointer client;

CommandRunner(BitwardenLibrary library, Pointer client) {
this.library = library;
this.client = client;
}

<T> T runCommand(Command command, Function<String, T> deserializer) {
String response = null;
try {
response = library.run_command(commandToString(command), client);
} catch (IOException e) {
throw new RuntimeException(e);
}
return deserializer.apply(response);
}

private String commandToString(Command command) throws IOException {
// Removes null properties from the generated converter output to avoid command errors
String inputJson = Converter.CommandToJsonString(command);

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

Object inputObject = mapper.readValue(inputJson, Object.class);
return mapper.writeValueAsString(inputObject);
}
}
Loading