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

Secrets endpoints and encryption #52

Merged
merged 23 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
28 changes: 18 additions & 10 deletions src/main/java/git/tracehub/pmo/controller/SecretController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import git.tracehub.pmo.controller.request.RqSecret;
import git.tracehub.pmo.controller.request.SecretFromReq;
import git.tracehub.pmo.secret.Key;
import git.tracehub.pmo.secret.Keys;
import git.tracehub.pmo.secret.Secret;
import git.tracehub.pmo.secret.Secrets;
import jakarta.validation.Valid;
Expand Down Expand Up @@ -49,39 +51,45 @@ public class SecretController {
*/
private final Secrets secrets;

/**
* Keys.
*/
private final Keys keys;

/**
* Constructor.
*
* @param secrets Secrets
* @param keys Keys
*/
public SecretController(@Qualifier("encryptedSecrets") final Secrets secrets) {
public SecretController(
@Qualifier("encryptedSecrets") final Secrets secrets,
final Keys keys
) {
this.secrets = secrets;
this.keys = keys;
}

/**
* Keys by project.
*
* @param project Project id
* @return Secret
* @return List of keys
*/
@GetMapping("/keys")
public List<Secret> keys(@RequestParam final UUID project) {
return this.secrets.keys(project);
public List<Key> byProject(@RequestParam final UUID project) {
return this.keys.byProject(project);
}

/**
* Secret value by key.
*
* @param project Project id
* @param key Key
* @return Secret
*/
@GetMapping
public Secret secret(
@RequestParam final UUID project,
@RequestParam final String key
) {
return this.secrets.value(project, key);
public Secret byValue(final Key key) {
return this.secrets.value(key);
}

/**
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/git/tracehub/pmo/secret/DefaultKeys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2023-2024 Tracehub.git
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to read
* the Software only. Permissions is hereby NOT GRANTED to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package git.tracehub.pmo.secret;

import com.jcabi.jdbc.JdbcSession;
import com.jcabi.jdbc.SingleOutcome;
import git.tracehub.pmo.project.SqlStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.sql.DataSource;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

/**
* Default keys.
*
* @checkstyle DesignForExtensionCheck (50 lines)
* @since 0.0.0
*/
@Component
@RequiredArgsConstructor
public class DefaultKeys implements Keys {

/**
* Datasource.
*/
private final DataSource source;

@Override
@SneakyThrows
public List<Key> byProject(final UUID project) {
return new JdbcSession(this.source)
.sql(
new SqlStatement("select-keys-by-project.sql").asString()
).set(project)
.select(
(rs, stmt) -> {
final List<Key> keys = new ArrayList<>(5);
while (rs.next()) {
keys.add(
new KeyOf(rs).value()
);
}
return keys;
}
);
}

@Override
@SneakyThrows
public boolean exists(final Key key) {
return new JdbcSession(this.source)
.sql(new SqlStatement("exists-key.sql").asString())
.set(key.getProject())
.set(key.getName())
.select(new SingleOutcome<>(Boolean.class));
}

}
42 changes: 4 additions & 38 deletions src/main/java/git/tracehub/pmo/secret/DefaultSecrets.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@
package git.tracehub.pmo.secret;

import com.jcabi.jdbc.JdbcSession;
import com.jcabi.jdbc.SingleOutcome;
import git.tracehub.pmo.exception.ResourceNotFoundException;
import git.tracehub.pmo.project.SqlStatement;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.sql.DataSource;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
Expand All @@ -47,38 +43,18 @@ public class DefaultSecrets implements Secrets {

@Override
@SneakyThrows
public List<Secret> keys(final UUID project) {
return new JdbcSession(this.source)
.sql(
new SqlStatement("select-secrets-by-project.sql").asString()
).set(project)
.select(
(rs, stmt) -> {
final List<Secret> secrets = new ArrayList<>(5);
while (rs.next()) {
secrets.add(
new KeyOf(rs).value()
);
}
return secrets;
}
);
}

@Override
@SneakyThrows
public Secret value(final UUID project, final String key) {
public Secret value(final Key key) {
return new JdbcSession(this.source)
.sql(
new SqlStatement("select-secret-by-key.sql").asString()
).set(project)
.set(key)
).set(key.getProject())
.set(key.getName())
.select(
(rs, stmt) -> {
if (!rs.next()) {
throw new ResourceNotFoundException(
"Secret with project = %s and key = %s not found"
.formatted(project, key)
.formatted(key.getProject(), key.getName())
);
}
return new SecretOf(rs).value();
Expand Down Expand Up @@ -122,14 +98,4 @@ public Secret update(final Scalar<Secret> secret) {
);
}

@Override
@SneakyThrows
public boolean exists(final UUID project, final String key) {
return new JdbcSession(this.source)
.sql(new SqlStatement("exists-secret.sql").asString())
.set(project)
.set(key)
.select(new SingleOutcome<>(Boolean.class));
}

}
28 changes: 8 additions & 20 deletions src/main/java/git/tracehub/pmo/secret/EncryptedSecrets.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package git.tracehub.pmo.secret;

import java.util.List;
import java.util.UUID;
import lombok.SneakyThrows;
import org.cactoos.Scalar;
import org.jasypt.util.text.TextEncryptor;
Expand All @@ -37,7 +35,7 @@ public class EncryptedSecrets implements Secrets {
/**
* Secrets.
*/
private final Secrets secrets;
private final Secrets origin;

/**
* Encryptor.
Expand All @@ -47,25 +45,20 @@ public class EncryptedSecrets implements Secrets {
/**
* Constructor.
*
* @param secrets Secrets
* @param origin Secrets
* @param encryptor Encryptor
*/
public EncryptedSecrets(
@Qualifier("validatedSecrets") final Secrets secrets,
@Qualifier("validatedSecrets") final Secrets origin,
final TextEncryptor encryptor
) {
this.secrets = secrets;
this.origin = origin;
this.encryptor = encryptor;
}

@Override
public List<Secret> keys(final UUID project) {
return this.secrets.keys(project);
}

@Override
public Secret value(final UUID project, final String key) {
final Secret secret = this.secrets.value(project, key);
public Secret value(final Key key) {
final Secret secret = this.origin.value(key);
return new Secret(
secret.getProject(),
secret.getKey(),
Expand All @@ -77,7 +70,7 @@ public Secret value(final UUID project, final String key) {
@SneakyThrows
public Secret create(final Scalar<Secret> secret) {
final Secret content = secret.value();
return this.secrets.create(
return this.origin.create(
() -> new Secret(
content.getProject(),
content.getKey(),
Expand All @@ -90,7 +83,7 @@ public Secret create(final Scalar<Secret> secret) {
@SneakyThrows
public Secret update(final Scalar<Secret> secret) {
final Secret content = secret.value();
return this.secrets.update(
return this.origin.update(
() -> new Secret(
content.getProject(),
content.getKey(),
Expand All @@ -99,9 +92,4 @@ public Secret update(final Scalar<Secret> secret) {
);
}

@Override
public boolean exists(final UUID project, final String key) {
return this.secrets.exists(project, key);
}

}
46 changes: 46 additions & 0 deletions src/main/java/git/tracehub/pmo/secret/Key.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2023-2024 Tracehub.git
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to read
* the Software only. Permissions is hereby NOT GRANTED to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package git.tracehub.pmo.secret;

import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;

/**
* Key of the secret.
*
* @checkstyle ConstantUsageCheck (30 lines)
* @since 0.0.0
*/
@Getter
@EqualsAndHashCode
@AllArgsConstructor
public class Key {

/**
* Project id.
*/
private final UUID project;

/**
* Name of the key.
*/
private final String name;

}
9 changes: 4 additions & 5 deletions src/main/java/git/tracehub/pmo/secret/KeyOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @since 0.0.0
*/
@RequiredArgsConstructor
public final class KeyOf implements Scalar<Secret> {
public final class KeyOf implements Scalar<Key> {

/**
* Result set.
Expand All @@ -38,11 +38,10 @@ public final class KeyOf implements Scalar<Secret> {

@Override
@SneakyThrows
public Secret value() {
return new Secret(
public Key value() {
return new Key(
UUID.fromString(this.set.getString("project")),
this.set.getString("key"),
""
this.set.getString("key")
);
}

Expand Down
Loading
Loading