Skip to content

Commit

Permalink
Add list-accounts for organization/tenant accounts.
Browse files Browse the repository at this point in the history
  • Loading branch information
weetmuts committed Jun 17, 2024
1 parent 6b61ef4 commit 3e2a056
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 209 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Fredrik Öhrström
Copyright (c) 2022-2024 Fredrik Öhrström

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
26 changes: 25 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# Copyright (C) 2022-2023 Fredrik Öhrström (spdx: MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

PREFIX?=/usr/local
SOURCES=$(sort $(wildcard src/*))
Expand All @@ -7,6 +27,10 @@ all: moln moln.html moln.pdf moln.1
moln: $(SOURCES)
@rm -f moln
@cat $(SOURCES) > moln
@echo "exit 0" >> moln
@echo "#TRANSFORMS" >> moln
@tar czf transforms.tgz transforms
@cat transforms.tgz >> moln
@chmod a+x moln
@echo "Built moln"

Expand All @@ -20,7 +44,7 @@ moln.html: moln moln_htmq_pre moln_htmq_post
@cp moln_htmq_pre moln.htmq
@./moln --output=htmq --list-help >> moln.htmq
@cat moln_htmq_post >> moln.htmq
@xmq moln.htmq > moln.html
@xmq moln.htmq to-html > moln.html
@echo "Built moln.html"

moln.tex: moln moln_tex_pre moln_tex_post
Expand Down
76 changes: 72 additions & 4 deletions moln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
#
# Copyright (C) 2022-2023 Fredrik Öhrström (spdx: MIT)
# Copyright (C) 2022-2024 Fredrik Öhrström (spdx: MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,6 +25,14 @@ MOLN=$(realpath $0)
verbose=false
debug=false

TMP_DIR=`mktemp -d`
function cleanup()
{
rm -rf $TMP_DIR
}

trap cleanup EXIT

if [ -z "$AWS_ACCOUNT" ]
then
if [ ! -f $HOME/.config/moln/moln.env ]
Expand Down Expand Up @@ -108,6 +116,16 @@ function debug
>2 echo "$@"
fi
}


function extract_transforms
{
local OFFSET
OFF=$(grep --byte-offset --only-matching --text '#TRANSFORMS' $MOLN | cut -f 1 -d : | tail -n 1)
let OFFSET=OFF+12
dd bs=1 skip=$OFFSET count=100000 if=$MOLN of=$TMP_DIR/transforms.tgz 2>/dev/null
tar xzf $TMP_DIR/transforms.tgz -C "$TMP_DIR"
}
cmds_BASIC="whoami"

function help_whoami
Expand Down Expand Up @@ -577,6 +595,31 @@ function cmd_aws_list_ips
{
$CMD_AWS_LIST_IPS | jq -c '.Addresses[]' | while IFS=$"\n" read -r info; do summarize_aws_ip "$info" ; done
}
# Copyright (C) 2024 Fredrik Öhrström license spdx: MIT

cmds_ORG="\
list-accounts\
"

function cmd_list_accounts
{
echo "List organization accounts."
}

function cmd_list_accounts_pre {
true
}

CMD_AWS_LIST_ACCOUNTS="aws organizations list-accounts"
function cmd_aws_list_accounts {
extract_transforms
>&2 printf "Fetching accounts..."
$CMD_AWS_LIST_ACCOUNTS > $TMP_DIR/list_accounts.json
>&2 printf "\33[2K\r"
CMD="cat $TMP_DIR/list_accounts.json | xmq transform --stringparam=current-date=$(date +%Y-%m-%d) $TMP_DIR/transforms/summarize_aws_account.xslq $OUTPUT_TRANSFORM"
eval "$CMD"
eval "$OUTPUT_CMD"
}
cmds_POLICY="list-policies"

function help_list_policies
Expand Down Expand Up @@ -944,7 +987,9 @@ function cmd_aws_show_vm {

CMD_AWS_LIST_VMS="aws ec2 describe-instances"
function cmd_aws_list_vms {
$CMD_AWS_LIST_VMS | jq -c '.Reservations[].Instances[]' | while IFS=$"\n" read -r info; do summarize_aws_vm "$info" ; done
# $CMD_AWS_LIST_VMS | jq -c '.Reservations[].Instances[]' | while IFS=$"\n" read -r info; do summarize_aws_vm "$info" ; done

aws ec2 describe-instances | xmq transform --stringparam=current-date=$(date +%Y-%m-%d) transforms/summarize_aws_vms.xslq transform transforms/textify.xslq to-text
}

function cmd_aws_ssh_to_vm
Expand Down Expand Up @@ -1166,10 +1211,10 @@ function cmd_aws_list_webapi_domains
#############################################################

# Extract all commands from the above set of command categories by parsing this file.
all_cmds="$(eval echo $(grep ^cmds_ "$MOLN" | sed 's/\([^=]*\)=.*/\$\1/'))"
all_cmds="$(eval echo $(grep -a ^cmds_ "$MOLN" | sed 's/\([^=]*\)=.*/\$\1/'))"

# Extract all command groups.
all_cmd_groups="$(grep ^cmds_ "$MOLN" | sed 's/\([^=]*\)=.*/\$\1/')"
all_cmd_groups="$(grep -a ^cmds_ "$MOLN" | sed 's/\([^=]*\)=.*/\$\1/')"

# Now check the cloud selected, or all of them.
all_clouds="all aws azure gcloud"
Expand All @@ -1184,6 +1229,22 @@ else
output=ascii
fi

if ! command -v xmq &> /dev/null
then
echo "You need to install xmq before using moln. https://libxmq.org"
exit 1
fi

LAST_ARG="${@: -1}"
OUTPUT_TRANSFORM="transform $TMP_DIR/transforms/textify.xslq to-text"
OUTPUT_CMD="true"

if [ "$LAST_ARG" = "browse" ] || [ "$LAST_ARG" = "br" ]
then
OUTPUT_TRANSFORM="to-html > /tmp/moln_browse.html"
OUTPUT_CMD="firefox /tmp/moln_browse.html"
fi

while :
do
if [[ ! "$1" =~ -[a-z_-]+ ]]
Expand Down Expand Up @@ -1467,3 +1528,10 @@ else
fi
) | eval $postfunc
fi
exit 0
#TRANSFORMS
��X�s�6�3�f�)�%`c(dxȴy`&w�4�M�2����QIܔ��+�i�q��]��<`��o��w�>������"�׳�ѳ���-]���1ںe��a����v���� �� #s?��+��$����a��C���?�=�Zr�w 2����N����nH��-��w�6ؿו��K�]H?����.bJ���� �n�#���h�5���G}>�3�� 4m�X�f+`S����ڟw��֩��s!���aO�G�1F|�t� �n� ^HaE�g�M[�w�� ��L�v�����1�=�I�&�si<@^�<�6A��s;�B�8�? +\^o5)��2��_b�t�];����5�K�̉���1�?OY�N�h��ݤ-?׫F
(�V�D��cJ�D�!���<����p���5Ԍs���0�4�/w�����-o>ܞ�q��9����׷ �����3Yن���Ð�ͭ�q�JlB��� ' . Q��'m�C������ ��&�0�A>�D��iDn�8$CA�%�bn_�W��S}V9�~Z��#�D��'~!2�5�h�s��Cg����G-�A�o�#��W�ΡJ&q�IfbF|��Ño��c��4�/2�g;�D�1�;�vYS;� Ϳ"�b�k�/]���R�:&P$w*6x4悁c*u���q�Qْ�1! C �v��/�����-
���c��⻴�a.݂)�-�/Ţ�b��d�!u�*j��>:�n6�J�r�r8
o����t�ј�v!x~}(����N^�����{��)v:��;*�v��'4��i̵D�sd��&<.�'�u�:?��Dž�^!�:�.��= ��8�N�UF�%�jk��+����
��Vͮ�� ��QAJj��u�d�\�۴�G�蹪�jo��T�Et���Ow���褐��{����t��Dzڽ�������n��(dIgtҥ}Ɍ �D �#b8��\�%び�P��M6�Z�&��Eo�tB�E���a�k��d��K��]����vj��ܜ;�öD�(#��c�����7� J�ٮ��[P5�ˡj��f�6���p3�+�e y���f��J���yћ���y�1ݫ�h���vi��#����:��*����*����~�1x�c(
Expand Down
121 changes: 0 additions & 121 deletions moln.1
Original file line number Diff line number Diff line change
Expand Up @@ -56,127 +56,6 @@ Create a subshell, within which you have access to a different role to gain priv
.br
\fB\-d\fR debug with a lot of information.
.br
.SH BASIC
\fBwhoami\fR Display active account/identity.
.br
·aws sts get-caller-identity
.br

·az account list
.br

·gcloud config list
.br

.SH BUCKET
\fBlist-buckets\fR List storage buckets, ie aws s3/gcloud storage/azure blobs.
.br
·aws s3api list-buckets --output json
.br

.SH COST
\fBlist-costs\fR Print a summary of monthly costs for the last 12 months.
.br
.SH DNS
\fBlist-hosted-zones\fR List zones the dns is configured to serve.
.br
·aws route53 --region us-east-1 list-hosted-zones
.br
\fBlist-dns-records\fR List dns records in a zone.
.br
·aws route53 list-resource-record-sets --hosted-zone-id=${ID}
.br
\fBupsert-dns-record NAME TYPE DEST\fR Insert or update a dns record.
.br
·aws route53 change-resource-record-sets --hosted-zone-id ${ID} --change-batch file://${FILE}
.br
\fBremove-dns-record NAME\fR Remove a dns record.
.br
·aws route53 change-resource-record-sets --hosted-zone-id ${ID} --change-batch file://${FILE}
.br
\fBlist-domains\fR List dns domains.
.br
·aws route53domains --region us-east-1 list-domains
.br

.SH GROUP
\fBlist-groups\fR List iam groups.
.br
·aws iam list-groups
.br

·az ad group list
.br
\fBlist-groups-for-user\fR List groups to which a user belongs.
.br
.SH CLI
\fBinstall\fR Install the specified cloud cli.
.br
.SH IP
\fBlist-ips\fR List allocated external ip numbers.
.br
·aws ec2 describe-addresses
.br

.SH POLICY
\fBlist-policies\fR List iam policies.
.br
·aws iam list-policies
.br

.SH ROLE
\fBassume-role ROLE SESSION\fR Start a subshell with the rights of the assumed role.
.br
·aws sts assume-role --role-arn "arn:aws:iam::${AWS_ACCOUNT}:role/$ROLE" --role-session-name "$SESSION"
.br
\fBlist-roles\fR List iam roles.
.br
·aws iam list-roles
.br

.SH SUBNET
\fBlist-subnets\fR List all subnets.
.br
·aws ec2 describe-subnets
.br

.SH USER
\fBlist-users\fR List users in cloud account.
.br
·aws iam list-users
.br

·az ad user list
.br

·gcloud iam service-accounts --format=json list
.br

.SH VM
\fBcreate-vm-from-template NAME TEMPLATE_NAME\fR Create a virtual machine based on an existing vm template.
.br
·aws ec2 run-instances --launch-template LaunchTemplateName=${TEMPLATE_NAME} --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=${NAME}}]"
.br

·az foo bar ${TEMPLATE_NAME} ${NAME}
.br
\fBdestroy-vm NAMEID NAMEID\fR Destroy a virtual machine.
.br
.SH VPC
\fBlist-vpcs\fR List virtual private clouds/networks, aka vpc:s and vnets.
.br
·aws ec2 describe-vpcs
.br

·az network vnet list
.br

.SH WEBAPI
\fBlist-webapi-domains\fR List domain names mapped to web apis (REST/HTTPs) routers.
.br
·aws apigatewayv2 get-domain-names
.br


.SH AUTHOR
Written by Fredrik Öhrström.
Expand Down
24 changes: 22 additions & 2 deletions moln.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"/><title>wmbusmeters editor</title><link type="text/css" href="style.css" rel="stylesheet"/></head><body><h1>Moln Cross Reference CLI Commands AWS/Azure/Gcloud</h1>Moln is a bash shell script to simplify the initial work with multiple cloud providers.<ul><li>Use it to quickly install the cloud providers cli</li><li>Use it to perform commands on several cloud providers at once.</li><li>Use it to learn how to work with different cloud provicer cli:s.</li></ul>Complicated cloud configuration is performed using the web interface.
After you have prepared (for example) a vm template, then you can start it with moln.<p></p><b>Examples:</b><pre>moln aws install</pre><pre>moln azure whoami</pre><pre>moln gcloud list-users</pre><pre>moln aws list-subnets</pre><pre>moln all list-vms</pre><pre>moln aws assume-role allaccess MYSESSION3</pre><pre>moln --list-help</pre><h2>BASIC</h2><cmd>whoami</cmd><help> Display active account/identity.</help><br/><pre class="cloud">aws sts get-caller-identity</pre><pre class="cloud">az account list</pre><pre class="cloud">gcloud config list</pre><h2>BUCKET</h2><cmd>list-buckets</cmd><help> List storage buckets, ie aws s3/gcloud storage/azure blobs.</help><br/><pre class="cloud">aws s3api list-buckets --output json</pre><h2>COST</h2><cmd>list-costs</cmd><help> Print a summary of monthly costs for the last 12 months.</help><br/><h2>DNS</h2><cmd>list-hosted-zones</cmd><help> List zones the dns is configured to serve.</help><br/><pre class="cloud">aws route53 --region us-east-1 list-hosted-zones</pre><cmd>list-dns-records</cmd><help> List dns records in a zone.</help><br/><pre class="cloud">aws route53 list-resource-record-sets --hosted-zone-id=${ID}</pre><cmd>upsert-dns-record NAME TYPE DEST</cmd><help> Insert or update a dns record.</help><br/><pre class="cloud">aws route53 change-resource-record-sets --hosted-zone-id ${ID} --change-batch file://${FILE}</pre><cmd>remove-dns-record NAME</cmd><help> Remove a dns record.</help><br/><pre class="cloud">aws route53 change-resource-record-sets --hosted-zone-id ${ID} --change-batch file://${FILE}</pre><cmd>list-domains</cmd><help> List dns domains.</help><br/><pre class="cloud">aws route53domains --region us-east-1 list-domains</pre><h2>GROUP</h2><cmd>list-groups</cmd><help> List iam groups.</help><br/><pre class="cloud">aws iam list-groups</pre><pre class="cloud">az ad group list</pre><cmd>list-groups-for-user</cmd><help> List groups to which a user belongs.</help><br/><h2>CLI</h2><cmd>install</cmd><help> Install the specified cloud cli.</help><br/><h2>IP</h2><cmd>list-ips</cmd><help> List allocated external ip numbers.</help><br/><pre class="cloud">aws ec2 describe-addresses</pre><h2>POLICY</h2><cmd>list-policies</cmd><help> List iam policies.</help><br/><pre class="cloud">aws iam list-policies</pre><h2>ROLE</h2><cmd>assume-role ROLE SESSION</cmd><help> Start a subshell with the rights of the assumed role.</help><br/><pre class="cloud">aws sts assume-role --role-arn "arn:aws:iam::${AWS_ACCOUNT}:role/$ROLE" --role-session-name "$SESSION"</pre><cmd>list-roles</cmd><help> List iam roles.</help><br/><pre class="cloud">aws iam list-roles</pre><h2>SUBNET</h2><cmd>list-subnets</cmd><help> List all subnets.</help><br/><pre class="cloud">aws ec2 describe-subnets</pre><h2>USER</h2><cmd>list-users</cmd><help> List users in cloud account.</help><br/><pre class="cloud">aws iam list-users</pre><pre class="cloud">az ad user list</pre><pre class="cloud">gcloud iam service-accounts --format=json list</pre><h2>VM</h2><cmd>create-vm-from-template NAME TEMPLATE_NAME</cmd><help> Create a virtual machine based on an existing vm template.</help><br/><pre class="cloud">aws ec2 run-instances --launch-template LaunchTemplateName=${TEMPLATE_NAME} --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=${NAME}}]"</pre><pre class="cloud">az foo bar ${TEMPLATE_NAME} ${NAME}</pre><cmd>destroy-vm NAMEID NAMEID</cmd><help> Destroy a virtual machine.</help><br/><h2>VPC</h2><cmd>list-vpcs</cmd><help> List virtual private clouds/networks, aka vpc:s and vnets.</help><br/><pre class="cloud">aws ec2 describe-vpcs</pre><pre class="cloud">az network vnet list</pre><h2>WEBAPI</h2><cmd>list-webapi-domains</cmd><help> List domain names mapped to web apis (REST/HTTPs) routers.</help><br/><pre class="cloud">aws apigatewayv2 get-domain-names</pre></body></html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>wmbusmeters editor</title>
<link type="text/css" href="style.css" rel="stylesheet">
</head>
<body>
<h1>Moln Cross Reference CLI Commands AWS/Azure/Gcloud</h1>Moln is a bash shell script to simplify the initial work with multiple cloud providers.<ul>
<li>Use it to quickly install the cloud providers cli</li>
<li>Use it to perform commands on several cloud providers at once.</li>
<li>Use it to learn how to work with different cloud provicer cli:s.</li>
</ul>Complicated cloud configuration is performed using the web interface.
After you have prepared (for example) a vm template, then you can start it with moln.<p></p>
<b>Examples:</b><pre>moln aws install</pre>
<pre>moln azure whoami</pre>
<pre>moln gcloud list-users</pre>
<pre>moln aws list-subnets</pre>
<pre>moln all list-vms</pre>
<pre>moln aws assume-role allaccess MYSESSION3</pre>
<pre>moln --list-help</pre>
</body>
</html>
Binary file modified moln.pdf
Binary file not shown.
Loading

0 comments on commit 3e2a056

Please sign in to comment.