Skip to content

Commit

Permalink
Move signature keys out of resources (#84) (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
lamtev authored Jun 11, 2020
1 parent febb375 commit 65cc6d6
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
*.der
*.pem
application-secrets.*
/keys
/repos
13 changes: 8 additions & 5 deletions api/src/main/java/org/accula/api/auth/jwt/crypto/EcKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.SneakyThrows;

import java.nio.file.Files;
import java.nio.file.Path;
import java.security.Key;
import java.security.KeyFactory;
import java.security.interfaces.ECPrivateKey;
Expand All @@ -21,23 +23,24 @@ private EcKeys() {
}

@SneakyThrows
private static <T extends Key, U extends KeySpec> T key(final byte[] keyBytes,
private static <T extends Key, U extends KeySpec> T key(final Path keyPath,
final Function<byte[], U> specProducer,
final BiFunction<KeyFactory, U, T> keyProducer) {
final var keyBytes = Files.readAllBytes(keyPath);
final var spec = specProducer.apply(keyBytes);
final var factory = KeyFactory.getInstance("EC");

return keyProducer.apply(factory, spec);
}

@SuppressWarnings("NullableProblems")
public static ECPrivateKey privateKey(final byte[] keyBytes) {
return (ECPrivateKey) key(keyBytes, PKCS8EncodedKeySpec::new, KeyFactory::generatePrivate);
public static ECPrivateKey privateKey(final Path keyPath) {
return (ECPrivateKey) key(keyPath, PKCS8EncodedKeySpec::new, KeyFactory::generatePrivate);
}

@SuppressWarnings("NullableProblems")
public static ECPublicKey publicKey(final byte[] keyBytes) {
return (ECPublicKey) key(keyBytes, X509EncodedKeySpec::new, KeyFactory::generatePublic);
public static ECPublicKey publicKey(final Path keyPath) {
return (ECPublicKey) key(keyPath, X509EncodedKeySpec::new, KeyFactory::generatePublic);
}

@FunctionalInterface
Expand Down
5 changes: 3 additions & 2 deletions api/src/main/java/org/accula/api/config/JwtProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.nio.file.Path;
import java.time.Duration;

@ConfigurationProperties("accula.jwt")
Expand All @@ -14,8 +15,8 @@ public final class JwtProperties {

@Data
public static final class Signature {
private String publicKey;
private String privateKey;
private Path publicKey;
private Path privateKey;
}

@Data
Expand Down
14 changes: 4 additions & 10 deletions api/src/main/java/org/accula/api/config/WebSecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientService;
Expand All @@ -33,7 +32,6 @@
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.WebFilter;

import java.io.IOException;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.util.Collections;
Expand Down Expand Up @@ -101,17 +99,13 @@ clientRegistrations, pathMatchers(GET, "/api/login/{registrationId}")))
}

@Bean
public ECPublicKey publicKey() throws IOException {
final var resource = new ClassPathResource(jwtProperties.getSignature().getPublicKey());
final var bytes = resource.getInputStream().readAllBytes();
return EcKeys.publicKey(bytes);
public ECPublicKey publicKey() {
return EcKeys.publicKey(jwtProperties.getSignature().getPublicKey());
}

@Bean
public ECPrivateKey privateKey() throws IOException {
final var resource = new ClassPathResource(jwtProperties.getSignature().getPrivateKey());
final var bytes = resource.getInputStream().readAllBytes();
return EcKeys.privateKey(bytes);
public ECPrivateKey privateKey() {
return EcKeys.privateKey(jwtProperties.getSignature().getPrivateKey());
}

@Bean
Expand Down
2 changes: 2 additions & 0 deletions api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ DB_PASS=postgres
DB_NAME=accula
REPOS_PATH=repos/
WEBHOOK_SECRET=accula
JWT_SIGNATURE_PUBLIC_KEY=keys/accula.public.der
JWT_SIGNATURE_PRIVATE_KEY=keys/accula.private.der
4 changes: 2 additions & 2 deletions api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ server:
accula:
jwt:
signature:
publicKey: accula.public.der
privateKey: accula.private.der
publicKey: ${JWT_SIGNATURE_PUBLIC_KEY}
privateKey: ${JWT_SIGNATURE_PRIVATE_KEY}
issuer: accula
expiresIn:
access: 1m
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3'
services:
web:
build: web
image: vaddya/accula_web
expose:
- 80
depends_on:
Expand Down Expand Up @@ -30,12 +30,12 @@ services:
- DB_PASS=postgres
- DB_NAME=accula
- REPOS_PATH=/app/repos/
- JWT_SIGNATURE_PUBLIC_KEY=/app/keys/accula.public.der
- JWT_SIGNATURE_PRIVATE_KEY=/app/keys/accula.private.der
volumes:
- ./repos:/app/repos
postgres:
image: postgres:11
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
Expand Down

0 comments on commit 65cc6d6

Please sign in to comment.