This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
forked from blacklabelops/logrotate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userGroupCreator.sh
85 lines (62 loc) · 2.16 KB
/
userGroupCreator.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
# DOCUMENTATION
# logrotate won't work with
# a) files with unknown users
# b) if unknown user, then the directory must have closed permissions
# due to the nature of our containers, most of our users don't map to other containers
# the ones that do map are accidental
# due to writing all logs to a single directory, we need to keep the logging directory open
# this script takes files and checks for UNKNOWN groups or users
# and creates users/groups on-the-fly if needed
# these users have no meaning and creating a new container will lead to new usernames
declare -r LOG_FILE_PATH="${1}"
set -e # exit all shells if script fails
set -u # exit script if uninitialized variable is used
set -o pipefail # exit script if anything fails in pipe
function createGroup(){
# create a new group assigned to the given file's gid
local -r file_path="${1}"
local -r gid="$( stat -c '%g' "${file_path}" )"
local -r group="fakegroup-$(date +%s)"
sleep 1 # allow date to lapse
addgroup \
-g "${gid}" \
-S "${group}"
}
function createUser(){
# create a new user assigned to the given file's uid
local -r file_path="${1}"
local -r uid="$( stat -c '%u' "${file_path}" )"
local -r user="fakeuser-$(date +%s)"
sleep 1 # allow date to lapse
adduser \
-S "${user}" \
-D \
-H \
-u "${uid}"
}
function processLogFile(){
# check the given file for an associated user and group
# if either an UNKNOWN group or user, a user and/or group is randomly generated
# with the uid/gid assigned to the file
local -r file_path="${1}"
local -r user="$( stat -c '%U' "${file_path}" )"
local -r group="$( stat -c '%G' "${file_path}" )"
if [[ "${group}" == 'UNKNOWN' ]]; then
createGroup "${file_path}" || true
fi
if [[ "${user}" == 'UNKNOWN' ]]; then
createUser "${file_path}" || true
fi
}
#########################
# MAIN ##################
#########################
function main(){
if [[ -f "${LOG_FILE_PATH}" ]]; then
processLogFile "${LOG_FILE_PATH}"
else
exit 1
fi
}
main