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

SB3 readiness #985

Merged
merged 13 commits into from
Sep 28, 2023
2 changes: 1 addition & 1 deletion .github/kts/build.main.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public val workflowBuild: Workflow = workflow(
name = "Set up JDK",
action = SetupJavaV3(
distribution = SetupJavaV3.Distribution.Corretto,
javaVersion = "11",
javaVersion = "17",
),
)
run(
Expand Down
64 changes: 31 additions & 33 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
# This file was generated using Kotlin DSL (.github/kts/build.main.kts).
# This file was generated using Kotlin DSL (.github/kts/.github/kts/build.main.kts).
# If you want to modify the workflow, please change the Kotlin file and regenerate this YAML file.
# Generated with https://github.com/krzema12/github-actions-kotlin-dsl
# Generated with https://github.com/krzema12/github-workflows-kt

name: Java CI

on:
push:

push: {}
jobs:
"build":
runs-on: "ubuntu-latest"
build:
runs-on: ubuntu-latest
steps:
- id: step-0
name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- id: step-1
name: Cache
uses: actions/cache@v3
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-maven-
- id: step-2
name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: 11
distribution: corretto
- id: step-3
name: Build with Maven
run: mvn -B install --file pom.xml
- id: step-4
name: CodecovAction
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
- id: step-0
name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- id: step-1
name: Cache
uses: actions/cache@v3
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-maven-
- id: step-2
name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: 17
distribution: corretto
- id: step-3
name: Build with Maven
run: mvn -B install --file pom.xml
- id: step-4
name: CodecovAction
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,9 @@ assertEquals("Peter",p2.name.get());
// of course you should rather use p2.name.orElse("unknown") or something rather than get, but you know all that from using Optional...
```

### Spring Boot Compatibility


| Library version | Spring Boot version |
|-----------------|---------------------|
| 1.x.x | 2.7+ |
| 2.x.x | 3.1+ |
8 changes: 4 additions & 4 deletions cryptoshred-cloud-aws/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>cryptoshred</artifactId>
<groupId>eu.prismacapacity</groupId>
<version>1.1.15-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand All @@ -20,9 +20,9 @@
</dependency>

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.12.558</version>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
<version>2.20.145</version>
</dependency>

<!-- test deps -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 PRISMA European Capacity Platform GmbH
* Copyright © 2020-2023 PRISMA European Capacity Platform GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,16 +15,16 @@
*/
package eu.prismacapacity.cryptoshred.cloud.aws;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ReturnValue;
import com.amazonaws.services.dynamodbv2.model.UpdateItemRequest;
import eu.prismacapacity.cryptoshred.core.CryptoAlgorithm;
import eu.prismacapacity.cryptoshred.core.CryptoSubjectId;
import eu.prismacapacity.cryptoshred.core.keys.CryptoKey;
import eu.prismacapacity.cryptoshred.core.keys.CryptoKeySize;
import java.nio.ByteBuffer;
import lombok.NonNull;
import lombok.Value;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.ReturnValue;
import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest;

@Value(staticConstructor = "of")
class CreateCryptoKeyRequest {
Expand All @@ -39,14 +39,15 @@ class CreateCryptoKeyRequest {
@NonNull private final String tableName;

UpdateItemRequest toDynamoRequest() {
return new UpdateItemRequest()
.withTableName(tableName)
.withKey(Utils.subjectIdToKeyAttributeMap(subjectId))
.withConditionExpression("attribute_not_exists(#k)")
.withUpdateExpression("SET #k = :v")
.withExpressionAttributeNames(Maps.of("#k", Utils.generateKeyPropertyName(algorithm, size)))
.withReturnValues(ReturnValue.ALL_NEW)
.withExpressionAttributeValues(
Maps.of(":v", new AttributeValue().withB(ByteBuffer.wrap(key.getBytes()))));
return UpdateItemRequest.builder()
.tableName(tableName)
.key(Utils.subjectIdToKeyAttributeMap(subjectId))
.conditionExpression("attribute_not_exists(#k)")
.updateExpression("SET #k = :v")
.expressionAttributeNames(Maps.of("#k", Utils.generateKeyPropertyName(algorithm, size)))
.returnValues(ReturnValue.ALL_NEW)
.expressionAttributeValues(
Maps.of(":v", AttributeValue.fromB(SdkBytes.fromByteArray(key.getBytes()))))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 PRISMA European Capacity Platform GmbH
* Copyright © 2020-2023 PRISMA European Capacity Platform GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,8 @@
*/
package eu.prismacapacity.cryptoshred.cloud.aws;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException;
import java.util.Optional;

import eu.prismacapacity.cryptoshred.core.CryptoAlgorithm;
import eu.prismacapacity.cryptoshred.core.CryptoEngine;
import eu.prismacapacity.cryptoshred.core.CryptoSubjectId;
Expand All @@ -25,10 +25,11 @@
import eu.prismacapacity.cryptoshred.core.keys.CryptoKeyRepository;
import eu.prismacapacity.cryptoshred.core.keys.CryptoKeySize;
import eu.prismacapacity.cryptoshred.core.metrics.CryptoMetrics;
import java.util.Optional;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.val;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException;

/**
* CryptoKeyRepository implementation based on AWS DynamoDB. Supports multiple keys (algorithm +
Expand All @@ -40,7 +41,7 @@
public class DynamoDBCryptoKeyRepository implements CryptoKeyRepository {
@NonNull private final CryptoEngine engine;

@NonNull private final AmazonDynamoDB dynamoDB;
@NonNull private final DynamoDbClient dynamoDB;

@NonNull private final CryptoMetrics metrics;

Expand All @@ -55,7 +56,7 @@ public Optional<CryptoKey> findKeyFor(

val getRequest = GetCryptoKeyRequest.of(subjectId, algorithm, size, tableName);

val item = metrics.timedFindKey(() -> dynamoDB.getItem(getRequest.toDynamoRequest()).getItem());
val item = metrics.timedFindKey(() -> dynamoDB.getItem(getRequest.toDynamoRequest()).item());

if (item == null) {
return Optional.empty();
Expand Down Expand Up @@ -83,7 +84,7 @@ protected CryptoKey createCryptoKey(
val result =
metrics.timedCreateKey(() -> dynamoDB.updateItem(createRequest.toDynamoRequest()));

val resultKey = Utils.extractCryptoKeyFromItem(algorithm, size, result.getAttributes());
val resultKey = Utils.extractCryptoKeyFromItem(algorithm, size, result.attributes());

if (!resultKey.isPresent()) {
// should never ever happen because that would indicate a broken DynamoDB API
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 PRISMA European Capacity Platform GmbH
* Copyright © 2020-2023 PRISMA European Capacity Platform GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,12 +15,12 @@
*/
package eu.prismacapacity.cryptoshred.cloud.aws;

import com.amazonaws.services.dynamodbv2.model.GetItemRequest;
import eu.prismacapacity.cryptoshred.core.CryptoAlgorithm;
import eu.prismacapacity.cryptoshred.core.CryptoSubjectId;
import eu.prismacapacity.cryptoshred.core.keys.CryptoKeySize;
import lombok.NonNull;
import lombok.Value;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;

@Value(staticConstructor = "of")
class GetCryptoKeyRequest {
Expand All @@ -33,10 +33,11 @@ class GetCryptoKeyRequest {
@NonNull private final String tableName;

GetItemRequest toDynamoRequest() {
return new GetItemRequest()
.withTableName(tableName)
.withKey(Utils.subjectIdToKeyAttributeMap(subjectId))
.withConsistentRead(true)
.withProjectionExpression(Utils.generateKeyPropertyName(algorithm, size));
return GetItemRequest.builder()
.tableName(tableName)
.key(Utils.subjectIdToKeyAttributeMap(subjectId))
.consistentRead(true)
.projectionExpression(Utils.generateKeyPropertyName(algorithm, size))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 PRISMA European Capacity Platform GmbH
* Copyright © 2020-2023 PRISMA European Capacity Platform GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,18 +15,19 @@
*/
package eu.prismacapacity.cryptoshred.cloud.aws;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import java.util.Map;
import java.util.Optional;

import eu.prismacapacity.cryptoshred.core.CryptoAlgorithm;
import eu.prismacapacity.cryptoshred.core.CryptoSubjectId;
import eu.prismacapacity.cryptoshred.core.keys.CryptoKey;
import eu.prismacapacity.cryptoshred.core.keys.CryptoKeySize;
import java.util.Map;
import java.util.Optional;
import lombok.val;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

class Utils {
static Map<String, AttributeValue> subjectIdToKeyAttributeMap(CryptoSubjectId subjectId) {
return Maps.of("subjectId", new AttributeValue(subjectId.getId().toString()));
return Maps.of("subjectId", AttributeValue.fromS(subjectId.getId().toString()));
}

static String generateKeyPropertyName(CryptoAlgorithm algorithm, CryptoKeySize size) {
Expand All @@ -42,12 +43,12 @@ static Optional<CryptoKey> extractCryptoKeyFromItem(
return Optional.empty();
}

val bytes = keyAttributeValue.getB();
val bytes = keyAttributeValue.b();

if (bytes == null) {
return Optional.empty();
}

return Optional.of(CryptoKey.fromBytes(bytes.array()));
return Optional.of(CryptoKey.fromBytes(bytes.asByteArray()));
}
}
Loading
Loading