# Securing Sensitive Data in Spring Boot with application.properties and Launch Files

<div><strong>Source</strong>: <a href='https://www.tuanh.net/blog/spring/securing-sensitive-data-in-spring-boot-with-applicationproperties-and-launch-files'>Securing Sensitive Data in Spring Boot with application.properties and Launch Files</a></div><div class="line-post"> 
 <h2 class="title-post">1. Understanding the Problem</h2> 
</div>
<div class="line-post">
  Spring Boot applications rely on application.properties or application.yml files to define environment-specific settings. These files might be bundled in deployment packages or referenced in launch files for runtime execution. 
</div>
<div class="images-post"> 
 <img width="80%" class="element_521140-image" src="https://resource.tuanh.net/article/image/ec5a7d91-6d72-4160-ad1d-bab815ff2ee1.png" alt="Image"> 
</div>
<div class="line-post"> 
 <h3 class="sub1">1.1 Why Is This a Problem?</h3> 
</div>
<div class="line-post">
  When sensitive data like database credentials or API keys is embedded directly in these files, the following risks emerge: 
</div>
<div class="line-post"> 
 <ul> 
  <li><strong>Version Control Exposure</strong>: Files containing sensitive information might inadvertently be committed to Git.</li> 
  <li><strong>Insider Threats</strong>: Anyone with access to deployment packages could misuse the secrets.</li> 
  <li><strong>Runtime Vulnerabilities</strong>: Plain text configurations are exposed if the application logs them.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h3 class="sub1">1.2 What Are Launch Files?</h3> 
</div>
<div class="line-post">
  Launch files, such as those used with Docker, Kubernetes, or custom scripts, often reference Spring Boot configuration files. These files specify runtime parameters, including environment variables or arguments, and act as a bridge between the application and its deployment environment. A misstep in launch file design can inadvertently expose sensitive data during runtime. 
</div>
<div class="line-post"> 
 <h2 class="title-post">2. Securing Sensitive Data in Spring Boot Configuration</h2> 
</div>
<div class="line-post">
  Let’s tackle the challenge by implementing robust methods to safeguard sensitive data. 
</div>
<div class="line-post"> 
 <h3 class="sub1">2.1 Externalizing Configuration with Environment Variables</h3> 
</div>
<div class="line-post">
  Environment variables offer a straightforward approach to keep sensitive data out of configuration files. 
</div>
<div class="line-post"> <strong>Example: Using Environment Variables in application.properties:</strong> 
</div>
<pre class="code"># application.properties<br>spring.datasource.url=jdbc:mysql://localhost:3306/mydb<br>spring.datasource.username=${DB_USERNAME}<br>spring.datasource.password=${DB_PASSWORD}</pre>
<div class="line-post">
  Set the environment variables securely in your launch file or system shell: 
</div>
<div class="line-post"> <strong>Linux/macOS:</strong> 
</div>
<pre class="code">export DB_USERNAME=myuser<br>export DB_PASSWORD=securepassword</pre>
<div class="line-post"> <strong>Windows (PowerShell):</strong> 
</div>
<pre class="code">$env:DB_USERNAME="myuser"<br>$env:DB_PASSWORD="securepassword"</pre>
<div class="line-post"> <strong>Advantages:</strong> 
</div>
<div class="line-post"> 
 <ul> 
  <li>Secrets are managed outside the codebase.</li> 
  <li>You avoid hardcoding sensitive data in your repository.</li> 
 </ul> 
</div>
<div class="line-post"> <strong>Launch File Integration</strong>: For Docker Compose: 
</div>
<pre class="code">services:<br>  app:<br>    environment:<br>      - DB_USERNAME=myuser<br>      - DB_PASSWORD=securepassword</pre>
<div class="line-post"> 
 <h3 class="sub1">2.2 Using Encrypted Properties with Jasypt</h3> 
</div>
<div class="line-post">
  Jasypt provides an elegant solution to encrypt sensitive data directly in application.properties while allowing runtime decryption. 
</div>
<div class="line-post"> <strong>Step 1: Add Jasypt Dependency Include</strong> the following dependency in your pom.xml: 
</div>
<pre class="code">&lt;dependency&gt;<br>    &lt;groupId&gt;com.github.ulisesbocchio&lt;/groupId&gt;<br>    &lt;artifactId&gt;jasypt-spring-boot-starter&lt;/artifactId&gt;<br>    &lt;version&gt;3.0.4&lt;/version&gt;<br>&lt;/dependency&gt;</pre>
<div class="line-post"> <strong>Step 2: Encrypt Sensitive Data</strong> Encrypt your password using the Jasypt CLI: 
</div>
<pre class="code">java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI <br>  input="securepassword" password="encryptionKey" algorithm=PBEWithMD5AndDES</pre>
<div class="line-post">
  Encrypted output: 
</div>
<pre class="code">ENC(nj+ksjC6/FK9lVZVXFo28g==)</pre>
<div class="line-post"> <strong>Step 3: Use the Encrypted Value</strong> Update application.properties: 
</div>
<pre class="code">spring.datasource.password=ENC(nj+ksjC6/FK9lVZVXFo28g==)</pre>
<div class="line-post"> <strong>Step 4: Set the Decryption Key</strong> Provide the decryption key as an environment variable in the launch file: 
</div>
<pre class="code">export JASYPT_ENCRYPTOR_PASSWORD=encryptionKey</pre>
<div class="line-post"> <strong>How It Works:</strong> At runtime, Jasypt decrypts the encrypted value using the specified key. 
</div>
<div class="line-post"> <strong>Launch File Integration:</strong> For Kubernetes: 
</div>
<pre class="code">apiVersion: apps/v1<br>kind: Deployment<br>spec:<br>  template:<br>    spec:<br>      containers:<br>      - name: app<br>        env:<br>        - name: JASYPT_ENCRYPTOR_PASSWORD<br>          value: "encryptionKey"</pre>
<div class="line-post"> 
 <h3 class="sub1">2.3 Masking Sensitive Data in Logs</h3> 
</div>
<div class="line-post">
  Even with encrypted properties or environment variables, sensitive data can still accidentally appear in logs during debugging or error reporting. Spring Boot provides mechanisms to control this. 
</div>
<div class="line-post"> <strong>Example: Masking Sensitive Data</strong> In your application.properties, specify keys to mask: 
</div>
<pre class="code">management.endpoint.env.keys-to-sanitize=PASSWORD,SECRET,KEY</pre>
<div class="line-post"> <strong>What This Does:</strong> 
</div>
<div class="line-post"> 
 <ul> 
  <li>Prevents sensitive values from being logged in plain text.</li> 
  <li>Replaces values with ****** in log output.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h2 class="title-post">3. Implementing Best Practices in Launch Files</h2> 
</div>
<div class="line-post">
  When integrating Spring Boot configuration into launch files, follow these best practices: 
</div>
<div class="line-post"> 
 <h3 class="sub1">3.1 Avoid Hardcoding Secrets</h3> 
</div>
<div class="line-post">
  Never embed sensitive data directly in launch files. Instead, reference secure storage mechanisms, such as: 
</div>
<div class="line-post"> 
 <ul> 
  <li>Environment variables.</li> 
  <li>Secret management tools like AWS Secrets Manager or HashiCorp Vault.</li> 
 </ul> 
</div>
<div class="line-post"> 
 <h3 class="sub1">3.2 Secure Access to Launch Files</h3> 
</div>
<div class="line-post">
  Restrict file permissions to minimize exposure: 
</div>
<pre class="code">chmod 600 launch.sh</pre>
<div class="line-post"> 
 <h3 class="sub1">3.3 Use Role-Based Access Control</h3> 
</div>
<div class="line-post">
  Ensure only authorized personnel can modify or execute launch files. 
</div>
<div class="line-post"> 
 <h2 class="title-post">4. Conclusion</h2> 
</div>
<div class="line-post">
  Securing sensitive data in Spring Boot configurations and launch files is critical for safeguarding your application and user trust. Whether leveraging environment variables, Jasypt encryption, or secure logging practices, these techniques form a solid foundation for protecting sensitive information. 
</div>
<div class="line-post">
  Remember, the real-world impact of a security breach can be devastating, so implementing these measures today could save you significant trouble tomorrow. If you have any questions or need further clarification, feel free to comment below! 
</div><p><strong>Read more at </strong>: <a href='https://www.tuanh.net/blog/spring/securing-sensitive-data-in-spring-boot-with-applicationproperties-and-launch-files'>Securing Sensitive Data in Spring Boot with application.properties and Launch Files</a></p>
