[type/__dma_auth] Improve documentation and handle duplicate lines better

The state explorer gained a new value "multiple" (it is not used anywhere, just
informative).
The code will only write a "should" line once and drop duplicate lines.
This commit is contained in:
Dennis Camera 2020-06-11 15:21:35 +02:00
parent ca9e011d50
commit 5b8ae33b4e
2 changed files with 23 additions and 9 deletions

View file

@ -17,8 +17,13 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>. # along with cdist. If not, see <http://www.gnu.org/licenses/>.
# #
# This explorer looks for lines matching the server parameter in dma's auth.conf # This explorer looks for a line matching the login and server parameters
# and reports the login and server fields (password is cksummed) # in dma's auth.conf and reports:
# present: a line matching login + host + password exists
# absent: no line matching login + host exists
# different_password: a line exists but with a different pasword
# multiple: multiple lines matching login + host exist
# (should never happen)
auth_conf=$("${__type_explorer}/auth_conf") auth_conf=$("${__type_explorer}/auth_conf")
test -r "${auth_conf}" || exit 0 test -r "${auth_conf}" || exit 0
@ -32,6 +37,7 @@ fi
awk -F'\n' -v server="${server}" ' awk -F'\n' -v server="${server}" '
function getvalue(path) { function getvalue(path) {
# Reads the first line of the file located at path and returns it.
getline < path getline < path
close(path) close(path)
return $0 return $0
@ -42,6 +48,7 @@ BEGIN {
parameter_dir = ENVIRON["__object"] "/parameter/" parameter_dir = ENVIRON["__object"] "/parameter/"
# Read the parameters of this object
host_param = getvalue(parameter_dir "server") host_param = getvalue(parameter_dir "server")
if (!host_param) host_param = ENVIRON["__object_id"] if (!host_param) host_param = ENVIRON["__object_id"]
login_param = getvalue(parameter_dir "login") login_param = getvalue(parameter_dir "login")
@ -72,10 +79,13 @@ BEGIN {
} }
host == host_param && login == login_param { host == host_param && login == login_param {
if (passwd == passwd_param) # a match…
state = "present" if (state == "absent") {
else state = ((passwd == passwd_param) ? "present" : "different_password")
state = "different_password" } else {
# report "multiple" to that the type can remove the duplicates.
state = "multiple"
}
} }
END { END {

View file

@ -78,6 +78,7 @@ test -f "${auth_conf}" || touch "${auth_conf}"
awk -F '\n' -v mode=$mode ' awk -F '\n' -v mode=$mode '
function getvalue(path) { function getvalue(path) {
# Reads the first line of the file located at path and returns it.
getline < path getline < path
close(path) close(path)
return $0 return $0
@ -124,11 +125,12 @@ host == host_param {
if (mode) { if (mode) {
# state_should == present # state_should == present
if (login == login_param && !written) { if (login == login_param && !written) {
# replace line if host and login match # replace line if host and login match (but only if no line has
# been written already -> no duplicates)
print_should() print_should()
written = 1 written = 1
next
} }
next
} else { } else {
# state_should == absent # state_should == absent
if (!login_param || login == login_param) { if (!login_param || login == login_param) {
@ -139,7 +141,9 @@ host == host_param {
} }
# leave other lines alone # leave other lines alone
{ print } {
print
}
END { END {
if (mode && !written) { if (mode && !written) {