-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from emalloy/em/124512076
add stdlib::setup_sudoers
- Loading branch information
Showing
9 changed files
with
128 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
#! /bin/bash | ||
|
||
# add example_user for test | ||
sudo useradd -m example_user1 | ||
|
||
# add another user for inverse test - \ | ||
# sudo should not work for this user | ||
sudo useradd -m example_user2 | ||
sudo sh -c 'echo testpassword2019 | passwd --stdin example_user2' | ||
|
||
stdlib::info "Checking whether to add any users to sudoers ..." | ||
stdlib::setup_sudoers | ||
|
||
URL=${URL:-"http://ifconfig.co/json"} | ||
stdlib::info "Fetching ${URL}" | ||
stdlib::cmd curl --silent "${URL}" | ||
echo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#! /bin/bash | ||
# Copyright 2018 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Read the project metadata key named "sudoers" and add each comma separated value to | ||
# the sudoers file. | ||
stdlib::setup_sudoers() { | ||
local user user_list sudoers_file | ||
user_list="$(stdlib::metadata_get -k 'project/attributes/sudoers')" | ||
|
||
if [[ -z "${user_list}" ]]; then | ||
stdlib::debug "Skipping sudoers setup. \ | ||
The value of the project metadata key named sudoers is empty. \ | ||
Set sudoers to a comma separated list to enable sudo \ | ||
support, e.g. sudoers=jmccune,pames" | ||
return 0 | ||
fi | ||
|
||
sudoers_file="/etc/sudoers" | ||
sudoers_d="/etc/sudoers.d" | ||
|
||
for user in ${user_list//,/ }; do | ||
if [[ -f "${sudoers_d}/${user}" ]] \ | ||
&& ( grep -q "^${user}"'\b' "${sudoers_d}/${user}" ) \ | ||
|| (grep -q "^${user}"'\b' "${sudoers_file}") ; then | ||
stdlib::debug "User ${user} is already in /etc/sudoers or \ | ||
/etc/sudoers.d/${user}, taking no action" | ||
else | ||
stdlib::info "Adding ${user} to /etc/sudoers.d/${user}" | ||
echo -e "${user}\tALL= (ALL)\tNOPASSWD: ALL" \ | ||
> "${sudoers_d}/${user}" \ | ||
&& chmod 0440 "${sudoers_d}/${user}" | ||
fi | ||
done | ||
|
||
if visudo -c ; then | ||
stdlib::info "sudoers config valid!" | ||
else | ||
stdlib::error "sudoers config invalid!" | ||
return 1 | ||
fi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright 2018 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
require 'retriable' | ||
|
||
control 'simple startup-script-custom' do | ||
title "With the simple example of startup-script-custom calling stdlib::info and stdlib::cmd" | ||
|
||
describe "gcloud ... get-serial-port-output startup-scripts-example1" do | ||
# Avoid racing against the instance boot sequence | ||
before :all do | ||
Retriable.retriable(tries: 20) do | ||
get_serial_port_output = "gcloud compute instances get-serial-port-output startup-scripts-example1" | ||
@cmd = command("#{get_serial_port_output} --project #{attribute('project_id')} --zone #{attribute('region')}-a") | ||
if not %r{systemd: Startup finished}.match(@cmd.stdout) | ||
raise StandardError, "Not found: 'systemd: Startup finished' in console output, cannot proceed" | ||
end | ||
end | ||
end | ||
|
||
subject do | ||
@cmd | ||
end | ||
|
||
its('exit_status') { should be 0 } | ||
its('stdout') { should match(%r{Info \[\d+\]: Adding example_user1 to /etc/sudoers}) } | ||
its('stdout') { should match(%r{Info \[\d+\]: sudoers config valid!}) } | ||
its('stdout') { should match('INFO startup-script: Return code 0.') } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters