Code Monkey home page Code Monkey logo

cloudiscovery's Introduction

Cloudiscovery

PyPI version Downloads codecov python version CircleCI Codacy Badge Hits-of-Code GitHub license

aws provider

Cloudiscovery helps you to analyze resources in your cloud (AWS/GCP/Azure/Alibaba/IBM) account. Now this tool only can check resources in AWS, but we are working to expand to other providers.

The tool consists of various commands to help you understand the cloud infrastructure.

Features

Diagrams

Commands can generate diagrams. When modelling them, we try to follow the principle:

Graphical excellence is that which gives to the viewer the greatest number of ideas in the shortest time with the least ink in the smallest space.

Edward Tufte

Report

The commands generate reports that can be used to further analyze resources.

CLI

  1. Run the cloudiscovery command with following options (if a region not pass, this script will try to get it from ~/.aws/credentials):

1.1 To detect AWS VPC resources (more on AWS VPC):

cloudiscovery aws-vpc [--vpc-id vpc-xxxxxxx] --region-name xx-xxxx-xxx [--profile-name profile] [--diagram [yes/no]] [--filter xxx] [--verbose]

1.2 To detect AWS policy resources (more on AWS Policy):

cloudiscovery aws-policy [--profile-name profile] [--diagram [yes/no]] [--filter xxx] [--verbose]

1.3 To detect AWS IoT resources (more on AWS IoT):

cloudiscovery aws-iot [--thing-name thing-xxxx] --region-name xx-xxxx-xxx [--profile-name profile] [--diagram [yes/no]] [--filter xxx] [--verbose]

1.4 To detect all AWS resources (more on AWS All):

cloudiscovery aws-all --region-name xx-xxxx-xxx [--profile-name profile] [--services xxx,xxx] [--filter xxx] [--verbose]

1.5 To check AWS limits per resource (more on AWS Limit):

cloudiscovery aws-limit --region-name xx-xxxx-xxx [--profile-name profile] [--services xxx,xxx] [--usage 0-100] [--verbose]

1.6 To run AWS security controls (experimental feature):

cloudiscovery aws-security --region-name xx-xxxx-xxx [--profile-name profile] [--commands x] [--verbose]
  1. For help use:
cloudiscovery [aws-vpc|aws-policy|aws-iot|aws-all|aws-limit] -h

Debbuging

Enabling verbose mode, it is possible to debug all calls to the providers endpoints and check possible problems.

Filtering

It's possible to filter resources by tags and resource type. To filter, add an option --filter <VALUE>, where <VALUE> can be:

  1. Name=tags.costCenter;Value=20000 - to filter resources by a tag name costCenter and with value 20000.
  2. Name=type;Value=aws_lambda_function to only list lambda functions.

It's possible to pass multiple values, to be able to select a value from a set. Values are split by : sign. If a desired value has a : sign, wrap it in ' signs e.g. --filter="Name=tags.costCenter;Value=20000:'20001:1'.

It is possible to pass multiple filter options, just pass -f filter_1 -f filter_2. In that case, the tool will return resources that match either of the filters

Useful CF tags:

  1. aws:cloudformation:stack-name - Stack name
  2. aws:cloudformation:stack-id - Stack id
  3. aws:cloudformation:logical-id - Logical id defined in CF template

Requirements and Installation

Installation

This tool has been written in Python3+ and AWS-CLI and it works on Linux, Windows and Mac OS.

Make sure the latest version of AWS-CLI is installed on your workstation, and other components needed, with Python pip already installed:

pip install -U cloudiscovery

Once a while after installation, there can be some issues related with a cache from older version being used by a newer version. In that case, it's recommended to remove directory ./assets/.cache.

AWS Credentials

Make sure you have properly configured your AWS-CLI with a valid Access Key and Region:

aws configure

More on credentials configuration: Configuration basics

AWS Permissions

The configured credentials must be associated to a user or role with proper permissions to do all checks. If you want to use a role with narrowed set of permissions just to perform cloud discovery, use a role from the following the CF template maintained by our team.

To further increase security, you can add a block to check aws:MultiFactorAuthPresent condition in AssumeRolePolicyDocument. More on using IAM roles in the configuration file.

(Optional) If you want to be able to switch between multiple AWS credentials and settings, you can configure named profiles and later pass profile name when running the tool.

Commands

Cloudiscovery provides a CLI to easily perform desired actions.

AWS VPC

Example of a diagram (diagrams.net supported):

diagrams logo

Following resources are checked in VPC command:

  • Autoscaling Group
  • Classic/Network/Application Load Balancer
  • Client VPN Endpoints
  • CloudHSM
  • DocumentDB
  • Directory Service
  • EC2 Instance
  • ECS
  • EFS
  • ElastiCache
  • Elasticsearch
  • EKS
  • EMR
  • IAM Policy
  • Internet Gateway (IGW)
  • Lambda
  • Media Connect
  • Media Live
  • Media Store Policy
  • MSK
  • NACL
  • NAT Gateway
  • Neptune
  • QuickSight
  • RDS
  • REST Api Policy
  • Route Table
  • S3 Policy
  • Sagemaker Notebook
  • Sagemaker Training Job
  • Sagemaker Model
  • Security Group
  • SQS Queue Policy
  • Site-to-Site VPN Connections
  • Subnet
  • Synthetic Canary
  • VPC Peering
  • VPC Endpoint
  • VPN Customer Gateways
  • Virtual Private Gateways
  • Workspace

The subnets are aggregated to simplify the diagram and hide infrastructure redundancies. There can be two types of subnet aggregates:

  1. Private* ones with a route 0.0.0.0/0 to Internet Gateway
  2. Public* ones without any route to IGW

If EC2 instances and ECS instances are part of an autoscaling group, those instances will be aggregated on a diagram.

More information: AWS WA, REL 2: How do you plan your network topology?

AWS Policy

Example of a diagram:

diagrams logo

Following resources are checked in Policy command:

  • AWS Principal that are able to assume roles
  • IAM Group
  • IAM Group to policy relationship
  • IAM Policy
  • IAM Role
  • IAM Role to policy relationship
  • IAM User
  • IAM User to group relationship
  • IAM User to policy relationship

Some roles can be aggregated to simplify the diagram. If a role is associated with a principal and is not attached to any named policy, will be aggregated.

More information: AWS WA, SEC 3: How do you manage permissions for people and machines?

AWS IoT

Example of a diagram:

diagrams logo

Following resources are checked in IoT command:

  • IoT Billing Group
  • IoT Certificates
  • IoT Jobs
  • IoT Policies
  • IoT Thing
  • IoT Thing Type

AWS All

A command to list ALL AWS resources.

Example of an HTML report:

diagrams logo

The command calls all AWS services (200+) and operations with name Describe, Get... and List... (500+).

The operations must be allowed to be called by permissions described in AWS Permissions.

Types of resources mostly cover Terraform types. It is possible to narrow down scope of the resources to ones related with a given service with parameter -s e.g. -s ec2,ecs,cloudfront,rds.

More information: AWS WA, COST 2: How do you govern usage?

AWS Limit

It's possible to check resources limits across various service in an account. This command implements over 60 limits checks.

Example of an HTML report:

diagrams logo

With --services value,value,value parameter, you can narrow down checks to just services that you want to check.

With --threshold 0-100 option, you can customize a minimum percentage threshold to start reporting a warning.

  • Services available
    • Acm
    • Amplify
    • Apigateway
    • Appmesh
    • Appsync
    • Autoscaling Plans
    • Batch
    • Chime
    • Code Artifact
    • Code Build
    • Code Commit
    • Code Deploy
    • Codeguru Reviewer
    • Codeguru Profiler
    • Cognito Federated Identities
    • Cloudformation
    • Cloud Map
    • CloudWatch Logs
    • Dynamodb
    • EBS
    • EC2
    • ECR
    • ECS
    • Elastic Inference
    • Elastic Filesystem
    • Elastic Beanstalk
    • Elastic Loadbalancing
    • Forecast
    • Fraud Detector
    • Gamelift
    • Glue
    • IAM
    • Inspector
    • Kendra
    • KMS
    • Media Connect
    • Media Live
    • Media Package
    • Metwork Manager
    • Polly
    • Qldb
    • Robomaker
    • Route53
    • Route53resolver
    • RDS
    • S3
    • SES
    • SNS
    • SWF
    • Transcribe
    • Translate
    • VPC

AWS has a default quota to all services. At the first time that an account is created, AWS apply this default quota to all services.
An administrator can ask to increase the quota value of a certain service via ticket. This command helps administrators detect those issues in advance.

More information: AWS WA, REL 1 How do you manage service limits?

AWS Security

This features is experimental, but now you can run commands to check and analyze some security issues. The following commands are available now:

  • Access key age
  • EBS Encryption enabled
  • EC2 IMDSV2 Check
  • DynamoDB PITR Enabled
  • Incoming SSH Disabled
  • Cloudtrail enabled

Regions outside of main partition

If you wish to analyze accounts in regions outside the main AWS partition (e.g. GovCloud or China), you should provide credentials (e.g. a profile) that are applicable to a given partition. It's not possible to analyze regions from multiple partitions.

Using a Docker container

To build docker container using Dockerfile

docker build -t cloudiscovery .

After build container, you must start container using follow command. The run command will mount a filesystem with your actual aws cli credentials, then you won't need configure aws cli again.

docker run \
-it \
--mount type=bind,source=$HOME/.aws/,target=/root/.aws/,readonly \
cloudiscovery \
/bin/bash
  • If you are using Diagram output and due to fact container is a slim image of Python image, you must run cloudiscovery with "--diagram no", otherwise you'll have an error about "xdg-open". The output file will be saved in "assets/diagrams".

Translate

This project support English and Portuguese (Brazil) languages. To contribute with a translation, follow this steps:

  • Create a folder inside locales folder with prefix of new idiom with appropiate locale code. Copy "locales/messages.pot" to locales/newfolder/LC_MESSAGES/.
  • To build ".mo" file running this command from project root folder:
python msgfmt.py -o locales/NEWFOLDER/LC_MESSAGES/messages.mo locales/NEWFOLDER/LC_MESSAGES/messages

Contributing

If you have improvements or fixes, we would love to have your contributions. Please use PEP 8 code style.

Development

When developing, it's recommended to use venv.

In order to create a venv on macOS and Linux:

python3 -m venv env

On Windows:

py -m venv venv
OR
python -v venv venv

Once installed, you need to activate the virtual environment. Activation will put specific paths for python and pip commands. On macOS and Linux call:

source venv/bin/activate

On Windows:

.\venv\Scripts\activate

Make sure you have installed pre-commit.

Install development requirements:

pip install -U -r requirements.txt -r requirements-dev.txt

Add precommit hooks:

pre-commit install

To run pre-commit hooks, you can issue the following command:

pre-commit run --all-files

Running cloudiscovery in development mode:

python cloudiscovery/__init__.py OPTIONS

To add new resources to check limit, please remove "assets/.cache/cache.db"

Making a release

  1. Update the version in cloudiscovery/__init__.py and create a new git tag with git tag $VERSION.
  2. Once you push the tag to GitHub with git push --tags, a new CircleCI build is triggered.

Similar projects and products

cloudiscovery's People

Contributors

leandrodamascena avatar meshuga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cloudiscovery's Issues

Enhance Route Tables reporting

Currently a route table is reported to be directly associated with a VPC but in practice, the relationship is much more complex, with the tables being associated with subnets. The tool should detect that relationship and report it properly.
There is also a concept of a default table, that should be reported.

(AWS) Old subnet id breaks discovery

If a resource stores a reference to a removed subnet, the tool stops working. Call to get subnet details should be wrapped with @exception annotation.

Such situation happens to SageMaker:

Collecting data from Sagemaker Notebook instances...

Error running check SAGEMAKERNOTEBOOK.get_resources. Error message An error occurred (InvalidSubnetID.NotFound) when calling the DescribeSubnets operation: The subnet ID 'subnet-0db79a1feafe30580' does not exist
Collecting data from Security Groups...

Add new network detection mode: Policy

Since the tool is able to detect VPC resources, there's a task to detect IoT resources, it would be possible to detect policy resources.

We can all agree IAM policies are complex, with a lot of moving parts and manual verification of evaluation logic is not easy. The tool can help to get an overview of policies within an account.

Similarly to #29, there can be a command to initiate policy reporting:

$ ./aws-network-discovery.py policy --vpc-id vpc-xxxxxxx --region-name xx-xxxx-xxx

(AWS All) Ability to detect required parameters

It should be possible by the tool to pass a single required parameter in supported listing operation.
The tool should rely on the first run on the methods that did not required any input.

(AWS) Error running check SYNTHETICSCANARIES.get_resources

Hi,

I get the following error when it's scanning.
Command I ran:
clouddiscovery aws-vpc --region-name eu-west-1 --profile-name XXXX
Running:
python --version
Python 3.7.5
pip --version
pip 20.0.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
pip show clouddiscovery
Name: clouddiscovery
Version: 2.0.508
Summary: The tool to help you discover resources in the cloud environment
Home-page: https://github.com/Cloud-Architects/cloud-discovery
Author: Cloud Architects
Author-email: None
License: Apache License 2.0
Location: /usr/local/lib/python3.7/site-packages
Requires: diagrams, boto3, ipaddress
Required-by:
aws --version
aws-cli/2.0.18 Python/3.8.3 Darwin/19.3.0 botocore/2.0.0dev22

Collecting data from SQS Queue Policy...
Collecting data from Subnets...

Error running check SYNTHETICSCANARIES.get_resources. Error message Unknown service: 'synthetics'. Valid service names are: accessanalyzer, acm, acm-pca, alexaforbusiness, amplify, apigateway, apigatewaymanagementapi, apigatewayv2, appconfig, application-autoscaling, application-insights, appmesh, appstream, appsync, athena, autoscaling, autoscaling-plans, backup, batch, budgets, ce, chime, cloud9, clouddirectory, cloudformation, cloudfront, cloudhsm, cloudhsmv2, cloudsearch, cloudsearchdomain, cloudtrail, cloudwatch, codebuild, codecommit, codedeploy, codeguru-reviewer, codeguruprofiler, codepipeline, codestar, codestar-connections, codestar-notifications, cognito-identity, cognito-idp, cognito-sync, comprehend, comprehendmedical, compute-optimizer, config, connect, connectparticipant, cur, dataexchange, datapipeline, datasync, dax, detective, devicefarm, directconnect, discovery, dlm, dms, docdb, ds, dynamodb, dynamodbstreams, ebs, ec2, ec2-instance-connect, ecr, ecs, efs, eks, elastic-inference, elasticache, elasticbeanstalk, elastictranscoder, elb, elbv2, emr, es, events, firehose, fms, forecast, forecastquery, frauddetector, fsx, gamelift, glacier, globalaccelerator, glue, greengrass, groundstation, guardduty, health, iam, imagebuilder, importexport, inspector, iot, iot-data, iot-jobs-data, iot1click-devices, iot1click-projects, iotanalytics, iotevents, iotevents-data, iotsecuretunneling, iotthingsgraph, kafka, kendra, kinesis, kinesis-video-archived-media, kinesis-video-media, kinesis-video-signaling, kinesisanalytics, kinesisanalyticsv2, kinesisvideo, kms, lakeformation, lambda, lex-models, lex-runtime, license-manager, lightsail, logs, machinelearning, macie, managedblockchain, marketplace-catalog, marketplace-entitlement, marketplacecommerceanalytics, mediaconnect, mediaconvert, medialive, mediapackage, mediapackage-vod, mediastore, mediastore-data, mediatailor, meteringmarketplace, mgh, migrationhub-config, mobile, mq, mturk, neptune, networkmanager, opsworks, opsworkscm, organizations, outposts, personalize, personalize-events, personalize-runtime, pi, pinpoint, pinpoint-email, pinpoint-sms-voice, polly, pricing, qldb, qldb-session, quicksight, ram, rds, rds-data, redshift, rekognition, resource-groups, resourcegroupstaggingapi, robomaker, route53, route53domains, route53resolver, s3, s3control, sagemaker, sagemaker-a2i-runtime, sagemaker-runtime, savingsplans, schemas, sdb, secretsmanager, securityhub, serverlessrepo, service-quotas, servicecatalog, servicediscovery, ses, sesv2, shield, signer, sms, sms-voice, snowball, sns, sqs, ssm, sso, sso-oidc, stepfunctions, storagegateway, sts, support, swf, textract, transcribe, transfer, translate, waf, waf-regional, wafv2, workdocs, worklink, workmail, workmailmessageflow, workspaces, xray
Collecting data from VPC Endpoints...
Collecting data from VPC Peering...

(AWS) Add new VPC resources

More services with Custom VPC.

  • Amazon Neptune
  • CloudHSM
  • SageMaker Notebook
  • SageMaker Training job
  • SageMaker Models

Add new network detection mode: IoT

Right now the tool detects resources within VPC but that's not everything that rund within AWS.
Another area that can need an ability to detect network is IoT.

Because VPC resources and IoT resources can be drawn as independent entities (with small overlap), there should be a separate command to detect either of those two.

For VPC detection the following command can be issued:

$ ./aws-network-discovery.py vpc --vpc-id vpc-xxxxxxx --region-name xx-xxxx-xxx

For IoT detection the following command can be issued:

$ ./aws-network-discovery.py iot --vpc-id vpc-xxxxxxx --region-name xx-xxxx-xxx

There could be overlaps e.g. an IoT Rule can call a Lambda Function but that is acceptable to have.

Refactor code

Refactor code to implement new enhancements and fix problems:

1 - PEP compliance
2 - New and fast way to instance classes to run checks
3 - General method to parse and analyze most of checks

(GENERAL) Rethink diagramming tool

Currently https://github.com/mingrammer/diagrams tool is being used for diagrams, which is a free, easy to use and extensive library. Although, during development of the cloudiscovery, we face the following issues with it:

  1. Security Groups, VPCs and other resources cannot be represented as areas, making diagrams more complex because of a need of edges to represent relations between resources (instead of just a placement of a resource in a designated area). Related issue: mingrammer/diagrams#162
  2. Usage of graphviz engine - although it is good for scientific documents, its UX is disputable. The project tweaks this engine (see mingrammer/diagrams#193) but the visualizations are worse in quality than commercial tools.\

The task is to think about ways to have good diagrams.

Add graph visualization export

Since we often have situations when network can get complex, it's often simpler to represent the network as a graph. A ready tool can be used for that purpose, which is cloudmapper.

Region problem

A code refactoring from # 5 generated a problem when region is not informed on profile file (~.aws/config). Region is not required when configure aws cli and if not informed region attribute doesn't exist on file.

image

List IAM policies

VPCs can be referred in IAM policies. It would be good to detect them.

AliCloud support

It would be good to have a command to discover Alicloud network resources.

(AWS Policy) Analyze policy statements

To better understand relations between a policy and resources, an analysis of statements should be made, with focus on principals.

The diagram could provide two types of connections:

  • One from a policy with Allow statement to a principal, maybe there should be a green line
  • One from a policy with Deny statement to a principal, perhaps a red line should be drawn

(AWS) Bug: Running script with region "all"

When run script with region "all" and there is no default region in ~./aws/config, botocore raise a exception.

python clouddiscovery\__init__.py aws-vpc -v vpc-XXXX -d True -r all

raise NoRegionError()
botocore.exceptions.NoRegionError: You must specify a region.

image

(AWS VPC) Show relations in subnet aggregations

I got Lambda functions that are related only to two out of three private subnets. Yet, on the diagram this is not represented and the lambda function goes to an aggregated 3 subnets, which is incorrect.

That should be further adjusted, so this subgroup is represented on the diagram. That can be done to e.g. display sub-subnets on relations.

Terraform support

It should be possible to read TF files, so that discovery of undeployed infrastructure can be made.

List VPCs

It would be good to list all VPCs, that is ones defined within a region as well as ones peered directly as well as through a transit gateway attachment.

(AWS) Better multi account support

A typical AWS deployment consists often of multiple AWS accounts/organization units.

To be able to properly analyze such deployment, it should be possible to ensure generated report and diagram names won't overlap across different accounts.

More compute resources

It would be good to list:

  • ECS Fargate clusters
  • EKS clusters
  • EMR clusters
  • MSK clusters
  • SQS Policies (like in #2)
  • ASGs (through subnets)

(AWS) Error running checks on EFS

Command: python clouddiscovery_init_.py aws-vpc -v vpc-XXXXX -d True -r us-east-1

Error message:

Collecting data from EFS Mount Targets...
Error running check EFS.get_resources. Error message 'Name'

(AWS VPC) More network resources

It would be good to have:

  • ALB resources
  • Internet Gateway
  • NAT Gateway
  • VPC/Service Endpoint
  • Route tables
  • Subnets
  • Peered connections
  • Security Groups
  • NACLs
  • Virtual Private Gateways
  • VPN Connections
  • Client VPN Endpoints

(AWS) Resource filtering by tags

Tags in AWS are used for various purposes to group resources [1].

It should be possible to filter out detected resources by tags, so that it would be possible to report only a specific group of resources instead of all resources within an account.

There can be an additional parameter for that purpose that would accept tag name and value(s) --filter="Name=tags.costCenter;Value=20000:'20001:1'". The mentioned example reports resources only with tag tags.costCenter with values 20000 and 20001:1.

[1] https://d1.awsstatic.com/whitepapers/aws-tagging-best-practices.pdf

Project license

An explicit license file is something good to have if it's about to be used for commercial purposes and encourage contributors to add improvements.
Recommended one: Apache License 2.0

(AWS) Add EC2 Target Groups

Target groups are crucial part of load balancers and ECS clusters. The tool should:

  1. List TGs
  2. List related load balancers
  3. List related ECS services

Add Media Services

These resources use custom VPC.

  • MediaConnect
  • MediaLive Inputs
  • MediaStore Container Policy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.