Skip to content

Commit 7c67e02

Browse files
committed
Refactor MinIO policy handling in scripts. Updated policy path to include bucket name in README and scripts. Introduced MINIO_POLICY_NAME variable for consistent policy naming across create-user and healthcheck scripts. Adjusted Dockerfile to remove hardcoded policy path.
1 parent ee1402d commit 7c67e02

File tree

5 files changed

+15
-27
lines changed

5 files changed

+15
-27
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ The following environment variables can be used to customize the MinIO user init
6161
| `MINIO_ALIAS` | Alias for the MinIO server | `minio` |
6262
| `MINIO_USER_BUCKET_PERMISSIONS` | Comma-separated list of bucket permissions | `s3:ListBucket,s3:GetBucketLocation,s3:ListBucketMultipartUploads` |
6363
| `MINIO_USER_OBJECT_PERMISSIONS` | Comma-separated list of object permissions | `s3:PutObject,s3:GetObject,s3:DeleteObject,s3:ListMultipartUploadParts,s3:AbortMultipartUpload` |
64-
| `MINIO_POLICY_PATH` | Path to the policy file. This file will be created if it doesn't exist or you can provide your own JSON by mounting to the `/policies` directory. | `/policies/readwrite-bucket.json` |
64+
| `MINIO_POLICY_PATH` | Path to the policy file. This file will be created if it doesn't exist or you can provide your own JSON by mounting to the `/policies` directory. | `/policies/readwrite-bucket-${MINIO_USER_BUCKET_NAME}.json` |
65+
| `MINIO_POLICY_NAME` | Name of the policy you want to create/update/overwrite in MinIO. If you don't provide this, we just use the file name of your policy (without the `.json`). | `basename "$MINIO_POLICY_PATH" .json` (and trimmed of any special characters) |
6566
| `DEBUG` | Enable debug mode | `false` |
6667
| `SLEEP` | Keep container running after initialization | `true` |
6768

src/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ ENV DEBUG=false \
77
MINIO_ADMIN_PASSWORD='' \
88
MINIO_ALIAS=minio \
99
MINIO_HOST='' \
10-
MINIO_POLICY_PATH='/policies/readwrite-bucket.json' \
1110
MINIO_USER_ACCESS_KEY='' \
1211
MINIO_USER_BUCKET_NAME='' \
1312
MINIO_USER_BUCKET_PERMISSIONS='s3:ListBucket,s3:GetBucketLocation,s3:ListBucketMultipartUploads' \

src/create-user.sh

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ if [ "$DEBUG" = "true" ]; then
1010
fi
1111

1212
mc_cmd="mc $debug_flag"
13-
minio_policy_name=$(basename "$MINIO_POLICY_PATH" .json | tr '[:upper:]' '[:lower:]' | tr -cd '[:alnum:]-_')
1413
################################################################################
1514
# Functions
1615
################################################################################
@@ -52,7 +51,7 @@ check_policy_exists() {
5251
local policy_list
5352
policy_list=$($mc_cmd admin policy list)
5453
case "$policy_list" in
55-
*"$minio_policy_name"*) return 0 ;;
54+
*"$MINIO_POLICY_NAME"*) return 0 ;;
5655
*) return 1 ;;
5756
esac
5857
}
@@ -80,13 +79,13 @@ $mc_cmd mb "$MINIO_ALIAS/$MINIO_USER_BUCKET_NAME" --ignore-existing
8079

8180
# Create policy if it doesn't exist
8281
if ! check_policy_exists; then
83-
echo "NOTICE: Policy $minio_policy_name not found. Creating..."
84-
$mc_cmd admin policy create "$MINIO_ALIAS" "$minio_policy_name" "$MINIO_POLICY_PATH"
82+
echo "NOTICE: Policy $MINIO_POLICY_NAME not found. Creating..."
83+
$mc_cmd admin policy create "$MINIO_ALIAS" "$MINIO_POLICY_NAME" "$MINIO_POLICY_PATH"
8584
fi
8685

8786
# Create user and apply policy
88-
$mc_cmd admin user add "$MINIO_ALIAS" "$MINIO_USER_ACCESS_KEY" "$minio_policy_name"
89-
$mc_cmd admin policy attach "$MINIO_ALIAS" "$minio_policy_name" --user "$MINIO_USER_ACCESS_KEY"
87+
$mc_cmd admin user add "$MINIO_ALIAS" "$MINIO_USER_ACCESS_KEY" "$MINIO_USER_SECRET_KEY"
88+
$mc_cmd admin policy attach "$MINIO_ALIAS" "$MINIO_POLICY_NAME" --user "$MINIO_USER_ACCESS_KEY"
9089

9190
# Sleep or exit
9291
sleep_or_exit

src/entrypoint.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/bin/bash
22
set -e
33
MINIO_ACCESS_KEY_EXISTS=false
4+
MINIO_POLICY_PATH=${MINIO_POLICY_PATH:-"/policies/readwrite-bucket-${MINIO_USER_BUCKET_NAME}.json"}
5+
MINIO_POLICY_NAME=$(basename "$MINIO_POLICY_PATH" .json | tr '[:upper:]' '[:lower:]' | tr -cd '[:alnum:]-_')
6+
7+
export MINIO_POLICY_PATH
8+
export MINIO_POLICY_NAME
49

510
if [ "$DEBUG" = "true" ]; then
611
set -x

src/healthcheck.sh

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,7 @@ if [ "$DEBUG" = "true" ]; then
77
fi
88

99
echo "Starting health check..."
10-
11-
# Get policy name from path
12-
minio_policy_name=$(basename "$MINIO_POLICY_PATH" .json | tr '[:upper:]' '[:lower:]' | tr -cd '[:alnum:]-_')
13-
echo "Checking for policy: $minio_policy_name"
14-
15-
# Check if alias exists
16-
echo "Checking MinIO alias..."
17-
alias_list=$(mc alias list)
18-
case "$alias_list" in
19-
*"$MINIO_ALIAS"*)
20-
echo "✅ MinIO alias found"
21-
;;
22-
*)
23-
echo "ERROR: MinIO alias $MINIO_ALIAS not found"
24-
exit 1
25-
;;
26-
esac
10+
echo "Checking for policy: $MINIO_POLICY_NAME"
2711

2812
# Check if user exists
2913
echo "Checking MinIO user..."
@@ -42,11 +26,11 @@ esac
4226
echo "Checking MinIO policy..."
4327
policy_list=$(mc admin policy list "$MINIO_ALIAS")
4428
case "$policy_list" in
45-
*"$minio_policy_name"*)
29+
*"$MINIO_POLICY_NAME"*)
4630
echo "✅ MinIO policy found"
4731
;;
4832
*)
49-
echo "ERROR: MinIO policy $minio_policy_name not found"
33+
echo "ERROR: MinIO policy $MINIO_POLICY_NAME not found"
5034
exit 1
5135
;;
5236
esac

0 commit comments

Comments
 (0)