Skip to main content

A command line utility for AWS profiles using dynamic authentication methods such as MFA.

Project description

hacksaws

PyPI version build status coverage report Code style: black Code style: flake8 Code style: mypy PyPI - License

A command line utility for AWS profiles using dynamic authentication methods such as MFA. The CLI allows for dynamic logins to update the credentials for an AWS profile temporarily, while storing the long-term access key and secret in a backup file until the next login or logout call is made. That way dynamic logins can be used while still maintaining the same functional credential interface as non-dynamic credentials.

At this time only MFA-based dynamic logins are supported, but SSO and others will be added in the future.

Usage

To login with MFA, execute the command:

$ hacksaws mfa login <PROFILE_NAME> <MFA_CODE>

There is a --lifespan flag that can be appended here to adjust the amount of time the profile login is valid for before it expires. The default is 12 hours (--lifetime=43200 seconds), but that can be adjusted to a maximum of 24 hours if the profile login allows authentication lifespans of that length.

ECR logins with docker can also be handled with the command by adding the --ecr flag. This will the local docker environment into ECR in the default AWS region for the specified profile.

$ hacksaws mfa login <PROFILE_NAME> <MFA_CODE> --ecr

It is also possible to login to ECR in multiple regions with the --ecr-region flag.

$ hacksaws mfa login <PROFILE_NAME> <MFA_CODE> \
    --ecr \
    --ecr-region=eu-central-1 \
    --ecr-region=us-west-2 \
    --ecr-region=ca-central-1

ECR will always log into the AWS default region. The --ecr-region flag allows for adding additional regions to the login command.

Then to log out:

$ hacksaws mfa logout <PROFILE_NAME>

It is possible to log in and out of ECR for the account with that profile as well by including the --ecr flag in the login call.

Alternate directories for the AWS credentials directory can be specified with the --directory flag.

And for separated AWS credentials directories in the home directory that follow the pattern ~/.aws-<NAME>, a --name flag can be specified to use that directory instead of the default ~/.aws directory. This is a useful pattern for separating profiles by account in cases where one has multiple account credentials.

Requiring MFA

Here's an example policy that allows a user to manage their own user account settings while requiring MFA.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ViewAccountInfo",
      "Effect": "Allow",
      "Action": [
        "iam:ListUsers",
        "iam:ListAccount*",
        "iam:ListMFADevices",
        "iam:GetAccountPasswordPolicy",
        "iam:GetAccountSummary"
      ],
      "Resource": "*"
    },
    {
      "Sid": "ViewAndManageTheirUser",
      "Effect": "Allow",
      "Action": [
        "iam:*LoginProfile",
        "iam:*AccessKey*",
        "iam:*SSHPublicKey*",
        "iam:*SigningCertificate*",
        "iam:*ServiceSpecificCredential*",
        "iam:GetUser",
        "iam:ChangePassword"
      ],
      "Resource": "arn:aws:iam::*:user/${aws:username}"
    },
    {
      "Sid": "ManageTheirOwnMFA",
      "Effect": "Allow",
      "Action": [
        "iam:CreateVirtualMFADevice",
        "iam:DeactivateMFADevice",
        "iam:DeleteVirtualMFADevice",
        "iam:EnableMFADevice",
        "iam:ListMFADevices",
        "iam:ListVirtualMFADevices",
        "iam:ResyncMFADevice"
      ],
      "Resource": [
        "arn:aws:iam::*:mfa/${aws:username}",
        "arn:aws:iam::*:user/${aws:username}"
      ]
    },
    {
      "Sid": "DenyAllExceptListedIfNoMFA",
      "Effect": "Deny",
      "NotAction": [
        "iam:ListUsers",
        "iam:ListMFADevices",
        "iam:ChangePassword",
        "iam:GetUser",
        "iam:CreateVirtualMFADevice",
        "iam:EnableMFADevice",
        "iam:ListMFADevices",
        "iam:ListVirtualMFADevices",
        "iam:ResyncMFADevice",
        "sts:GetSessionToken"
      ],
      "Resource": "*",
      "Condition": {
        "BoolIfExists": {
          "aws:MultiFactorAuthPresent": "false"
        }
      }
    }
  ]
}

Controlling password quality and expiration policies is an account-level requirement and more details can be found at (Setting an account password policy for IAM users)[https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_passwords_account-policy.html]

Additional Resources:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hacksaws-0.3.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hacksaws-0.3.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file hacksaws-0.3.0.tar.gz.

File metadata

  • Download URL: hacksaws-0.3.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/5.15.154+

File hashes

Hashes for hacksaws-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9408d0add6af2704d85e168427244324d5a35c5e376bfd15dd3229dfc259fb1e
MD5 8a2d8d936e313be6f64fc5f864ec4b6d
BLAKE2b-256 48a8d7e80b6ff4fb2e0469a6670d5cfb2ce7d17a775cfdb6dcdea9d4575bec39

See more details on using hashes here.

File details

Details for the file hacksaws-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: hacksaws-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/5.15.154+

File hashes

Hashes for hacksaws-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fdc6e0c6459987927912fc7175c71b25657d8aa468687f16d32c408ae2970c13
MD5 53fdd5e03ea3fbcbb4161556fbe24b4a
BLAKE2b-256 7ec3a99f716cbc580d2a4cdc6865c9040b3e896ddad1966dadd35cad4ba0244a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page