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

26 use mariadb + split repository layer #27

Merged
merged 6 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
80 changes: 22 additions & 58 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,69 +26,33 @@ services:



cassandra-extractor:
image: busybox:latest
container_name: cassandra-extractor
volumes:
- ./docker/cassandra_exporter/datastax-mcac-agent-0.3.5.tar.gz:/opt/datastax-mcac-agent-0.3.5.tar.gz
- cassandra_mcac:/opt/datastax-mcac-agent-0.3.5
command: sh -c "tar -xzf /opt/datastax-mcac-agent-0.3.5.tar.gz -C /opt/"


d1r1n1:
image: cassandra:4.0.12
container_name: d1r1n1
environment: &environment
CASSANDRA_SEEDS: d1r1n1
CASSANDRA_CLUSTER_NAME: C1
CASSANDRA_DC: D1
CASSANDRA_RACK: R1
CASSANDRA_ENDPOINT_SNITCH: GossipingPropertyFileSnitch
CASSANDRA_NUM_TOKENS: 128
JVM_OPTS: -javaagent:/opt/datastax-mcac-agent-0.3.5/lib/datastax-mcac-agent.jar
mariadb:
image: 'mariadb:11.3.2'
container_name: mariadb
environment:
- 'MARIADB_DATABASE=hicha'
- 'MARIADB_PASSWORD=secret'
- 'MARIADB_ROOT_PASSWORD=verysecret'
- 'MARIADB_USER=myuser'
ports:
- "9042:9042"
- "9103:9103"
depends_on:
cassandra-extractor:
condition: service_completed_successfully
volumes: &mcac
- cassandra_mcac:/opt/datastax-mcac-agent-0.3.5
healthcheck:
test: [ "CMD-SHELL", "[ $$(nodetool statusgossip) = running ]" ]

- '3306:3306'
Comment on lines +29 to +38
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure secure default settings for MariaDB.

The environment variables for MariaDB, such as MARIADB_PASSWORD and MARIADB_ROOT_PASSWORD, are set to simple values which might not be secure. Consider using more complex passwords and managing them through secure means like Docker secrets or environment variables.


# deploy:
# resources:
# limits:
# cpus: '1' # Minimum CPU requirement for MariaDB (10% of one core)
# memory: 256M # Minimum memory requirement for MariaDB (256MB)

d1r1n2:
image: cassandra:4.0.12
container_name: d1r1n2
ports:
- "9043:9042"
- "9104:9103"
environment:
<<: *environment

volumes: *mcac
depends_on:
d1r1n1:
condition: service_healthy
cassandra-extractor:
condition: service_completed_successfully

d1r1n3:
image: cassandra:4.0.12
container_name: d1r1n3
mariadb-exporter:
container_name: mariadb-exporter
image: 'prom/mysqld-exporter'
restart: always
command:
- "--mysqld.username=root:verysecret"
- "--mysqld.address=host.docker.internal:3306"
ports:
- "9044:9042"
- "9105:9103"
environment:
<<: *environment
volumes: *mcac
depends_on:
d1r1n1:
condition: service_healthy
cassandra-extractor:
condition: service_completed_successfully
- "9104:9104"
Comment on lines +47 to +55
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

The configuration of the mariadb-exporter in the compose.yaml file reveals a potential security issue:

  • The credentials are passed directly in the command line arguments ("--mysqld.username=root:verysecret"), which is generally considered insecure as it can expose sensitive information in process listings and logs.

It is recommended to use more secure methods for handling credentials, such as environment variables or configuration files that are not exposed.

Analysis chain

Verify the configuration of the mariadb-exporter for security.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Check the security settings of the mariadb-exporter service.

# Test: Search for security best practices in the configuration of mariadb-exporter. Expect: Secure practices like non-root user, minimal permissions.
rg --type yaml $'mariadb-exporter' | rg 'user|permissions'

Length of output: 57



Script:

#!/bin/bash
# Description: Check for secure handling of credentials and network settings in mariadb-exporter configuration.

# Test: Search for any instance of credentials being passed in command lines and check for network exposure settings.
rg --type yaml --context 5 $'mariadb-exporter' | rg 'command|ports'

Length of output: 92



Script:

#!/bin/bash
# Description: Extract the full command line arguments for mariadb-exporter to assess how credentials are handled.

# Test: Search for the complete command line arguments in the mariadb-exporter configuration.
rg --type yaml --context 10 $'mariadb-exporter' | rg 'command'

Length of output: 85




Expand Down
5 changes: 5 additions & 0 deletions data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
insert into users(username) value ('huyvu'), ('admin'), ('testuser');
insert into conversations () value ();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SQL statement for inserting conversations is empty and does not specify values. Please clarify if this is intentional or an oversight.

INSERT INTO user_conversations (user_id, conversation_id) VALUES ((select users.user_id from users where username = 'huyvu' limit 1), (select conversation_id from conversations limit 1));
insert into messages (conversation_id, sender_id, message_text)
values (1, 1, 'This is a test message.');
57 changes: 20 additions & 37 deletions hicha-business/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
<groupId>io.huyvu.hicha</groupId>
<artifactId>hicha-backend</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>io.huyvu.hicha</groupId>
<artifactId>hicha-business</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hicha-business</name>
Expand All @@ -19,7 +17,6 @@
</properties>
<dependencies>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -39,27 +36,11 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>

<!--
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>runtime</scope>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
Expand Down Expand Up @@ -97,18 +78,6 @@
</dependency>


<!--<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mariadb</artifactId>
<scope>test</scope>
</dependency>-->

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>cassandra</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
Expand All @@ -135,6 +104,20 @@
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

<!--start repository-->
<dependency>
<groupId>io.huyvu.hicha</groupId>
<artifactId>hicha-repository</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.huyvu.hicharepositoryimpl</groupId>
<artifactId>hicha-repository-impl</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<!--end repository-->

</dependencies>

<build>
Expand All @@ -147,7 +130,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>io.huyvu.hicha.hichabusiness.HichaBusinessApplication</mainClass>
<mainClass>io.huyvu.hicha.HichaBusinessApplication</mainClass>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
Expand Down Expand Up @@ -197,8 +180,8 @@
</executions>
<configuration>
<excludes>
<exclude>io/huyvu/hicha/hichabusiness/HichaBusinessApplication.class</exclude>
<exclude>io/huyvu/hicha/hichabusiness/config/nativebuild/*</exclude>
<exclude>io/huyvu/hicha/HichaBusinessApplication.class</exclude>
<exclude>io/huyvu/hicha/config/nativebuild/*</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.huyvu.hicha;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HichaBusinessApplication {
public static void main(String[] args) {
SpringApplication.run(HichaBusinessApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.huyvu.hicha.hichabusiness.config;
package io.huyvu.hicha.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.huyvu.hicha.hichabusiness.config;
package io.huyvu.hicha.config;

import org.apache.coyote.ProtocolHandler;
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.huyvu.hicha.controller;

import io.huyvu.hicha.model.ConversationDetails;
import io.huyvu.hicha.repository.model.Message;
import io.huyvu.hicha.repository.repo.MessageRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;

@RestController
@RequestMapping("api/v1/message")
@RequiredArgsConstructor
public class MessageController {
private final MessageRepository messageRepository;

@PostMapping
void sendMessage(@RequestBody Message message) {
if(message.getSentAt() == null){
message.setSentAt(Instant.now());
}
messageRepository.save(message);
}

@GetMapping("{id}")
ConversationDetails getConversationDetails(@PathVariable Long id) {
var messages = messageRepository.findByConversationId(id);
return ConversationDetails.builder()
.conversationId(id)
.conversationName("Conversation " + id)
.messages(messages)
.build();
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading