Skip to content

Commit de9303f

Browse files
rameeshmRamesh Mani
andauthored
RANGER-5365:Add test users into Ranger Docker Base Image (#6)
* RANGER-5365:Add test users into Ranger Docker Base Image Signed-off-by: Ramesh Mani <rmani@apache.org> * RANGER-5365:Add test users into Ranger Docker Base Image - co-pilot review fix * RANGER-5365:Add test users into Ranger Docker Base Image -review comment fix * RANGER-5365:Add test users into Ranger Docker Base Image -build issue fix * RANGER-5365:Add test users into Ranger Docker Base Image -build issue fix2 --------- Signed-off-by: Ramesh Mani <rmani@apache.org> Co-authored-by: Ramesh Mani <rmani@apache.org>
1 parent 16615a4 commit de9303f

2 files changed

Lines changed: 99 additions & 16 deletions

File tree

docker/Dockerfile

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,16 @@ ENV RANGER_SCRIPTS=/home/ranger/scripts
4747
ENV RANGER_HOME=/opt/ranger
4848
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
4949

50-
# setup groups, users, directories
51-
RUN groupadd ranger \
52-
&& for u in ranger rangeradmin rangerusersync rangertagsync rangerkms; do \
53-
useradd -g ranger -ms /bin/bash $u; \
54-
done
55-
56-
RUN groupadd hadoop \
57-
&& for u in hdfs yarn hive hbase kafka ozone; do \
58-
useradd -g hadoop -ms /bin/bash $u; \
59-
done
60-
61-
RUN groupadd knox \
62-
&& useradd -g knox -ms /bin/bash knox
63-
64-
# setup directories
50+
# create directories and setup perms
6551
RUN mkdir -p /home/ranger/dist /home/ranger/scripts /opt/ranger && \
66-
chown -R ranger:ranger /home/ranger /opt/ranger && \
6752
chmod +rx /home/ranger /home/ranger/dist /home/ranger/scripts
6853

54+
# setup groups and users
55+
COPY docker/create_users_and_groups.sh ${RANGER_SCRIPTS}
56+
RUN chmod +x /home/ranger/scripts/create_users_and_groups.sh && \
57+
./home/ranger/scripts/create_users_and_groups.sh
58+
59+
# change ownerships
60+
RUN chown -R ranger:ranger /home/ranger /opt/ranger
61+
6962
ENTRYPOINT [ "/bin/bash" ]

docker/create_users_and_groups.sh

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/bin/bash
2+
3+
# Licensed to the Apache Software Foundation (ASF) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The ASF licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
# Script to create users and groups in ranger containers
20+
# This script is designed to be run during container initialization
21+
22+
set -e
23+
24+
# General-purpose function to create a group if it doesn't exist.
25+
create_group_if_not_exists() {
26+
local groupname=$1
27+
28+
if ! getent group "$groupname" &>/dev/null; then
29+
echo "Creating group: $groupname"
30+
groupadd "$groupname"
31+
echo "Group $groupname created successfully"
32+
else
33+
echo "Group $groupname already exists"
34+
fi
35+
}
36+
37+
# General-purpose function to create a user if it doesn't exist.
38+
create_user_if_not_exists() {
39+
local username=$1
40+
local home_dir=$2
41+
local primary_group=$3
42+
43+
if ! id "$username" &>/dev/null; then
44+
echo "Creating user: $username"
45+
useradd -g "$primary_group" -m -d "$home_dir" -s /bin/bash "$username"
46+
47+
# Set a default password
48+
echo "$username:$username" | chpasswd
49+
50+
echo "User $username created successfully"
51+
else
52+
echo "User $username already exists"
53+
fi
54+
}
55+
56+
# Function to create users and groups if not exist
57+
create_users_and_groups() {
58+
local group_name=$1
59+
local users=$2
60+
61+
echo "Creating group '$group_name' with users: $users"
62+
63+
# Create group and users
64+
create_group_if_not_exists "$group_name"
65+
for u in $users; do
66+
create_user_if_not_exists "$u" "/home/$u" "$group_name"
67+
done
68+
}
69+
70+
# Main function to create all users and groups if not exist
71+
create_all_users_and_groups() {
72+
echo "Starting user and group creation..."
73+
74+
# Create ranger group and users
75+
create_users_and_groups "ranger" "ranger rangeradmin rangerusersync rangertagsync rangerkms rangerauditserver"
76+
77+
# Create hadoop group and users
78+
create_users_and_groups "hadoop" "hdfs yarn hive hbase kafka ozone"
79+
80+
# Create knox group and user
81+
create_users_and_groups "knox" "knox"
82+
83+
# Create test users in test group
84+
create_users_and_groups "testgroup" "testuser1 testuser2 testuser3"
85+
86+
echo "User and group creation completed successfully..."
87+
}
88+
89+
# Execute the main function
90+
create_all_users_and_groups

0 commit comments

Comments
 (0)