Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
snf-image-creator
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
itminedu
snf-image-creator
Commits
788ea15b
Commit
788ea15b
authored
10 years ago
by
Nikos Skalkotos
Browse files
Options
Downloads
Patches
Plain Diff
Always check if a file exists before opening it
It is better to create warnings that RuntimeError exceptions.
parent
130e9f64
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
image_creator/os_type/bsd.py
+5
-0
5 additions, 0 deletions
image_creator/os_type/bsd.py
image_creator/os_type/linux.py
+46
-37
46 additions, 37 deletions
image_creator/os_type/linux.py
with
51 additions
and
37 deletions
image_creator/os_type/bsd.py
+
5
−
0
View file @
788ea15b
...
...
@@ -29,6 +29,11 @@ class Bsd(Unix):
def
_cleanup_password
(
self
):
"""
Remove all passwords and lock all user accounts
"""
if
not
self
.
image
.
g
.
is_file
(
'
/etc/master.passwd
'
):
self
.
out
.
warn
(
"
File: `/etc/master.passwd
'
is missing. Nothing to do!
"
)
return
master_passwd
=
[]
for
line
in
self
.
image
.
g
.
cat
(
'
/etc/master.passwd
'
).
splitlines
():
...
...
This diff is collapsed.
Click to expand it.
image_creator/os_type/linux.py
+
46
−
37
View file @
788ea15b
...
...
@@ -72,49 +72,54 @@ class Linux(Unix):
def
_remove_user_accounts
(
self
):
"""
Remove all user accounts with id greater than 1000
"""
if
'
USERS
'
not
in
self
.
meta
:
return
# Remove users from /etc/passwd
passwd
=
[]
removed_users
=
{}
metadata_users
=
self
.
meta
[
'
USERS
'
].
split
()
for
line
in
self
.
image
.
g
.
cat
(
'
/etc/passwd
'
).
splitlines
():
fields
=
line
.
split
(
'
:
'
)
if
int
(
fields
[
2
])
>
1000
:
removed_users
[
fields
[
0
]]
=
fields
# remove it from the USERS metadata too
if
fields
[
0
]
in
metadata_users
:
metadata_users
.
remove
(
fields
[
0
])
else
:
passwd
.
append
(
'
:
'
.
join
(
fields
))
self
.
meta
[
'
USERS
'
]
=
"
"
.
join
(
metadata_users
)
# Delete the USERS metadata if empty
if
not
len
(
self
.
meta
[
'
USERS
'
]):
del
self
.
meta
[
'
USERS
'
]
self
.
image
.
g
.
write
(
'
/etc/passwd
'
,
'
\n
'
.
join
(
passwd
)
+
'
\n
'
)
# Remove users from /etc/passwd
if
self
.
image
.
g
.
is_file
(
'
/etc/passwd
'
):
passwd
=
[]
metadata_users
=
self
.
meta
[
'
USERS
'
].
split
()
for
line
in
self
.
image
.
g
.
cat
(
'
/etc/passwd
'
).
splitlines
():
fields
=
line
.
split
(
'
:
'
)
if
int
(
fields
[
2
])
>
1000
:
removed_users
[
fields
[
0
]]
=
fields
# remove it from the USERS metadata too
if
fields
[
0
]
in
metadata_users
:
metadata_users
.
remove
(
fields
[
0
])
else
:
passwd
.
append
(
'
:
'
.
join
(
fields
))
# Remove the corresponding /etc/shadow entries
shadow
=
[]
for
line
in
self
.
image
.
g
.
cat
(
'
/etc/shadow
'
).
splitlines
():
fields
=
line
.
split
(
'
:
'
)
if
fields
[
0
]
not
in
removed_users
:
shadow
.
append
(
'
:
'
.
join
(
fields
))
self
.
meta
[
'
USERS
'
]
=
"
"
.
join
(
metadata_users
)
self
.
image
.
g
.
write
(
'
/etc/shadow
'
,
"
\n
"
.
join
(
shadow
)
+
'
\n
'
)
# Delete the USERS metadata if empty
if
not
len
(
self
.
meta
[
'
USERS
'
]):
del
self
.
meta
[
'
USERS
'
]
# Remove the corresponding /etc/group entries
group
=
[]
for
line
in
self
.
image
.
g
.
cat
(
'
/etc/group
'
).
splitlines
():
fields
=
line
.
split
(
'
:
'
)
# Remove groups tha have the same name as the removed users
if
fields
[
0
]
not
in
removed_users
:
group
.
append
(
'
:
'
.
join
(
fields
))
self
.
image
.
g
.
write
(
'
/etc/passwd
'
,
'
\n
'
.
join
(
passwd
)
+
'
\n
'
)
else
:
self
.
out
.
warn
(
"
File: `/etc/passwd
'
is missing.
"
"
No users were deleted
"
)
return
self
.
image
.
g
.
write
(
'
/etc/group
'
,
'
\n
'
.
join
(
group
)
+
'
\n
'
)
if
self
.
image
.
g
.
is_file
(
'
/etc/shadow
'
):
# Remove the corresponding /etc/shadow entries
shadow
=
[]
for
line
in
self
.
image
.
g
.
cat
(
'
/etc/shadow
'
).
splitlines
():
fields
=
line
.
split
(
'
:
'
)
if
fields
[
0
]
not
in
removed_users
:
shadow
.
append
(
'
:
'
.
join
(
fields
))
self
.
image
.
g
.
write
(
'
/etc/shadow
'
,
"
\n
"
.
join
(
shadow
)
+
'
\n
'
)
else
:
self
.
out
.
warn
(
"
File: `/etc/shadow
'
is missing.
"
)
if
self
.
image
.
g
.
is_file
(
'
/etc/group
'
):
# Remove the corresponding /etc/group entries
group
=
[]
for
line
in
self
.
image
.
g
.
cat
(
'
/etc/group
'
).
splitlines
():
fields
=
line
.
split
(
'
:
'
)
# Remove groups tha have the same name as the removed users
if
fields
[
0
]
not
in
removed_users
:
group
.
append
(
'
:
'
.
join
(
fields
))
self
.
image
.
g
.
write
(
'
/etc/group
'
,
'
\n
'
.
join
(
group
)
+
'
\n
'
)
# Remove home directories
for
home
in
[
field
[
5
]
for
field
in
removed_users
.
values
()]:
...
...
@@ -216,6 +221,10 @@ class Linux(Unix):
going to shrink the image you should probably disable this.
"""
if
not
self
.
image
.
g
.
is_file
(
'
/etc/fstab
'
):
self
.
out
.
warn
(
"
File: `/etc/fstab
'
is missing. No entry removed!
"
)
return
new_fstab
=
""
fstab
=
self
.
image
.
g
.
cat
(
'
/etc/fstab
'
)
for
line
in
fstab
.
splitlines
():
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment