简体   繁体   中英

Spring cloud loading keystore from classpath vs file

I have a spring cloud config server that uses keystore to decrypt values from git server. If I reference the keystore using file path, it works as expected and decrypts the {cipher} values. However, if I try to load the keystore from classpath it stops working with this error : CipherEnvironmentEncryptor.decrypt - Cannot decrypt key: username (class java.lang.IllegalStateException: Cannot load keys from store: class path resource [mykey.p12])

Im setting the encrypt properties on the class instead of yaml since I need to lookup different passwords from external vault system for dev/prod keystores.
I can also see p.12 file under target/classes after the build, so it is not filtered out during the build. Not sure what I'm missing.

    SpringApplication sa = new SpringApplication(Myclass.class);
    Properties springProperties = new Properties();
    springProperties.setProperty("encrypt.enabled", "true");
    springProperties.setProperty("encrypt.key-store.location", "file:///Users/user/IdeaProjects/project/src/main/resources/configuration/mykey.p12"); //working fine
    springProperties.setProperty("encrypt.key-store.location", "classpath:/configuration/mykey.p12");  //does not work
    springProperties.setProperty("encrypt.key-store.type", "PKCS12");
    springProperties.setProperty("encrypt.key-store.password", "password");
    springProperties.setProperty("encrypt.key-store.secret", "password");
    springProperties.setProperty("encrypt.key-store.alias", "vault");

    sa.setDefaultProperties(springProperties);
    sa.run(args);

Using

<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.2.0.RELEASE</version>
<name>spring-cloud-config-server</name>

My issue was actually related to this : generated certificate stops working when moved to resources folder

I had maven filtering configuration on the resources which was corrupting the final p12 file. For now I just moved files that need filtering to another resource directory and it works.

<resources>
    <resource>
        <directory>src/main/resources</directory>
    </resource>
     <resource>
         <directory>src/main/resources-filtered</directory>
          <filtering>true</filtering>
            <includes>
              <include>*.yml</include>
              <include>logback.xml</include>
            </includes>
      </resource>
  </resources>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM