#!/bin/bash
#
# Copyright (c) 2019-2020 Oracle and/or its affiliates. All rights reserved.
# Licensed under the GNU General Public License Version 2 as shown at https://oss.oracle.com/licenses/GPL-2.

whitelist="
kubeadm-[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}
kubelet-[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}
kubectl-[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}
helm-[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}
olcne-[a-zA-Z-]+-chart-[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}
olcne-nginx-[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}
keepalived-[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}
istio-istioctl-[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}
"

# Block multiple packages being installed at the same time
invalid_chars=( ' ' ',' )

for invalid_char in "${invalid_chars[@]}"; do
    if echo "$1" | grep -q "${invalid_char}"; then
       	echo "'$1' contains an invalid character: '${invalid_char}'"
	    exit 1
    fi
done

match=false
# Check if we have a whitelist match
for whitelist_item in ${whitelist}; do
    if echo "$1" | grep -E "${whitelist_item}"; then
        match=true
        break
    fi
done

if [[ ${match} == false ]]; then
	echo "$1 is not permitted to be installed by this tool"
	exit 1
fi

yum install -y "$1"
exit $?
