Skip to content

Commit

Permalink
Merge pull request #49 from KingAkeem/improving_codebase
Browse files Browse the repository at this point in the history
Improving codebase
  • Loading branch information
PSNAppz authored Nov 15, 2024
2 parents 3d5a03f + d1d626d commit c8bae58
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 16 deletions.
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.19

WORKDIR /app

# Downloading dependencies
COPY go.mod go.sum ./
RUN go mod download

# Copying over source code
COPY . /app

# Compiling code
RUN CGO_ENABLED=0 GOOS=linux go build -o shadowguard cmd/main.go
CMD ["./shadowguard"]
39 changes: 33 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,41 @@ The architecture also facilitates both active and passive modes of operation, al
TODO: Instructions on how to setup "ShadowGuard", its dependencies, and how to get it running.

### Database
- Run files within `sql` directory.
- Grant usage on the `public` schema to `gorm`.
1. Login as a postgres user `psql -U postgres`
2. Connect to `gorm` database, `\connect gorm`
3. Grant usage on the `public` schema for `gorm`, `GRANT USAGE on SCHEMA "public" to gorm;`
Run `build.sh` to setup install the necessary dependencies including Postgresql and configures the gorm database.

This script does a couple of things in this sequence.
1. Installs all of the necessary dependencies.
2. Starts the Postgres service.
3. Runs the necessary SQL commands to create the user and database needed for gorm.
4. Configures Postgres to allow md5 connections.

```shell
sudo bash build.sh
```

## How to Use:
TODO: Instructions on how to integrate "ShadowGuard" with other applications.
The program can be ran in a multitde of ways.

### Go
```shell
go run cmd/main.go
```

### Shell
```shell
chmod +x run.sh
./run.sh
```

### Docker
```shell
docker build . -t shadow_guard
docker run --network=host shadow_guard
```

## Unit Tests:

In order to run unit tests, you can use the shell script `run_tests.sh` in the root directory. The unit tests can be ran using convential Go commands.

## Documentation:
TODO: Link to full API documentation, or brief outline of main methods and how to use them.
Expand Down
74 changes: 71 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,72 @@
#! /bin/bash
#!/bin/bash

psql -U postgres -f sql/initialize_database.sql
psql -U postgres -d gorm -c "GRANT USAGE ON SCHEMA public TO gorm;"
# Variables
DB_USER="gorm"
DB_PASSWORD="gorm"
DB_NAME="gorm"

# Check if PostgreSQL is installed; if not, install it
if ! command -v psql &> /dev/null; then
echo "PostgreSQL is not installed. Installing..."
sudo apt update
sudo apt install -y postgresql postgresql-contrib
else
echo "PostgreSQL is already installed."
fi

# Start PostgreSQL service
sudo systemctl start postgresql
sudo systemctl enable postgresql

# Switch to the postgres user and run SQL commands to create user and database
sudo -u postgres psql <<EOF
-- Create a new PostgreSQL user with password
DO \$$
BEGIN
IF NOT EXISTS (
SELECT FROM pg_catalog.pg_roles
WHERE rolname = '${DB_USER}'
) THEN
CREATE USER ${DB_USER} WITH PASSWORD '${DB_PASSWORD}';
END IF;
END
\$$;
-- Create a new PostgreSQL database owned by the new user
DO \$$
BEGIN
IF NOT EXISTS (
SELECT FROM pg_catalog.pg_database
WHERE datname = '${DB_NAME}'
) THEN
CREATE DATABASE ${DB_NAME} OWNER ${DB_USER};
END IF;
END
\$$;
EOF

# Variables
PG_HBA_PATH="/var/lib/pgsql/data/pg_hba.conf"
BACKUP_PATH="/var/lib/pgsql/backups/pg_hba.conf.bak"

# Backup the original pg_hba.conf file
if [ ! -f "$BACKUP_PATH" ]; then
echo "Backing up the original pg_hba.conf to $BACKUP_PATH"
sudo cp "$PG_HBA_PATH" "$BACKUP_PATH"
else
echo "Backup already exists at $BACKUP_PATH"
fi

# Update authentication method to md5
echo "Updating authentication method to md5 in $PG_HBA_PATH"
sudo sed -i 's/^\(local\s\+all\s\+all\s\+\)peer/\1md5/' "$PG_HBA_PATH"
sudo sed -i 's/^\(host\s\+all\s\+all\s\+127.0.0.1\/32\s\+\)ident/\1md5/' "$PG_HBA_PATH"
sudo sed -i 's/^\(host\s\+all\s\+all\s\+::1\/128\s\+\)ident/\1md5/' "$PG_HBA_PATH"

# Restart PostgreSQL to apply changes
echo "Restarting PostgreSQL service to apply changes..."
sudo systemctl restart postgresql

echo "Authentication method updated to md5. Please test your connection."

echo "User and database setup complete."
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"database": {
"host": "localhost",
"port": "5433",
"port": "5432",
"user": "gorm",
"password": "gorm",
"dbname": "gorm"
Expand Down
2 changes: 2 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /bin/bash
go run cmd/main.go
61 changes: 61 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

# Default values for flags
VERBOSE=false
COVERAGE=false
PACKAGE=""
RACE=false
TIMEOUT=""

# Help message
usage() {
echo "Usage: $0 [options]"
echo "Options:"
echo " -v Enable verbose output for each test"
echo " -c Generate a coverage report"
echo " -p <package> Specify package to test (default is all packages)"
echo " -r Enable race condition detection"
echo " -t <time> Set a custom timeout (e.g., 2m, 1h)"
echo " -h Display this help message"
}

# Parse command-line options
while getopts "vcp:rt:h" opt; do
case ${opt} in
v ) VERBOSE=true ;;
c ) COVERAGE=true ;;
p ) PACKAGE=$OPTARG ;;
r ) RACE=true ;;
t ) TIMEOUT=$OPTARG ;;
h ) usage; exit 0 ;;
* ) usage; exit 1 ;;
esac
done

# Base command
CMD="go test"

# Add flags based on options
[ "$VERBOSE" = true ] && CMD+=" -v"
[ "$RACE" = true ] && CMD+=" -race"
[ -n "$TIMEOUT" ] && CMD+=" -timeout $TIMEOUT"
[ -n "$PACKAGE" ] && CMD+=" $PACKAGE" || CMD+=" ./..."

# Coverage option
if [ "$COVERAGE" = true ]; then
COVERAGE_FILE="coverage.out"
CMD+=" -coverprofile=$COVERAGE_FILE"
fi

# Run the command
echo "Running command: $CMD"
$CMD

# Show coverage report if generated
if [ "$COVERAGE" = true ]; then
echo "Coverage report:"
go tool cover -func=$COVERAGE_FILE

# Optional: Display HTML report
# go tool cover -html=$COVERAGE_FILE
fi
6 changes: 0 additions & 6 deletions sql/initialize_database.sql

This file was deleted.

0 comments on commit c8bae58

Please sign in to comment.