GithubHelp home page GithubHelp logo

gojek / cureiam Goto Github PK

View Code? Open in Web Editor NEW
71.0 24.0 8.0 1.09 MB

Clean accounts over permissions in GCP infra at scale

License: Apache License 2.0

Python 99.12% Dockerfile 0.65% Makefile 0.23%
gcp iam cloud

cureiam's Introduction

CureIAM

Clean up of over permissioned IAM accounts on GCP infra in an automated way

CureIAM is an easy-to-use, reliable, and performant engine for Least Privilege Principle Enforcement on GCP cloud infra. It enables DevOps and Security team to quickly clean up accounts in GCP infra that have granted permissions of more than what are required. CureIAM fetches the recommendations and insights from GCP IAM recommender, scores them and enforce those recommendations automatically on daily basic. It takes care of scheduling and all other aspects of running these enforcement jobs at scale. It is built on top of GCP IAM recommender APIs and Cloudmarker framework.

Key features

Discover what makes CureIAM scalable and production grade.

  • Config driven : The entire workflow of CureIAM is config driven. Skip to Config section to know more about it.
  • Scalable : Its is designed to scale because of its plugin driven, multiprocess and multi-threaded approach.
  • Handles Scheduling: Scheduling part is embedded in CureIAM code itself, configure the time, and CureIAM will run daily at that time note.
  • Plugin driven: CureIAM codebase is completely plugin oriented, which means, one can plug and play the existing plugins or create new to add more functionality to it.
  • Track actionable insights: Every action that CureIAM takes, is recorded for audit purpose, It can do that in file store and in elasticsearch store. If you want you can build other store plugins to push that to other stores for tracking purposes.
  • Scoring and Enforcement: Every recommendation that is fetch by CureIAM is scored against various parameters, after that couple of scores like safe_to_apply_score, risk_score, over_privilege_score. Each score serves a different purpose. For safe_to_apply_score identifies the capability to apply recommendation on automated basis, based on the threshold set in CureIAM.yaml config file.

Usage

Since CureIAM is built with python, you can run it locally with these commands. Before running make sure to have a configuration file ready in either of /etc/CureIAM.yaml, ~/.CureIAM.yaml, ~/CureIAM.yaml, or CureIAM.yaml and there is Service account JSON file present in current directory with name preferably cureiamSA.json. This SA private key can be named anything, but for docker image build, it is preferred to use this name. Make you to reference this file in config for GCP cloud.

# Install necessary dependencies
$ pip install -r requirements.txt

# Run CureIAM now
$ python -m CureIAM -n

# Run CureIAM process as schedular
$ python -m CureIAM

# Check CureIAM help
$ python -m CureIAM --help

CureIAM can be also run inside a docker environment, this is completely optional and can be used for CI/CD with K8s cluster deployment.

# Build docker image from dockerfile
$ docker build -t cureiam . 

# Run the image, as schedular
$ docker run -d cureiam 

# Run the image now
$ docker run -f cureiam -m cureiam -n 

Config

CureIAM.yaml configuration file is the heart of CureIAM engine. Everything that engine does it does it based on the pipeline configured in this config file. Let's break this down in different sections to make this config look simpler.

  1. Let's configure first section, which is logging configuration and scheduler configuration.
  logger:
    version: 1

    disable_existing_loggers: false

    formatters:
      verysimple:
        format: >-
            [%(process)s]
            %(name)s:%(lineno)d - %(message)s
        datefmt: "%Y-%m-%d %H:%M:%S"

    handlers:
      rich_console:
        class: rich.logging.RichHandler
        formatter: verysimple

      file:
        class: logging.handlers.TimedRotatingFileHandler
        formatter: simple
        filename: /tmp/CureIAM.log
        when: midnight
        encoding: utf8
        backupCount: 5

    loggers:
      adal-python:
        level: INFO

    root:
      level: INFO
      handlers:
        - rich_console
        - file

  schedule: "16:00"

This subsection of config uses, Rich logging module and schedules CureIAM to run daily at 16:00.

  1. Next section is configure different modules, which we MIGHT use in pipeline. This falls under plugins section in CureIAM.yaml. You can think of this section as declaration for different plugins.
  plugins:
    gcpCloud:
      plugin: CureIAM.plugins.gcp.gcpcloud.GCPCloudIAMRecommendations
      params:
        key_file_path: cureiamSA.json

    filestore:
      plugin: CureIAM.plugins.files.filestore.FileStore

    gcpIamProcessor:
      plugin: CureIAM.plugins.gcp.gcpcloudiam.GCPIAMRecommendationProcessor
      params:
        mode_scan: true
        mode_enforce: true
        enforcer:
          key_file_path: cureiamSA.json
          allowlist_projects:
            - alpha
          blocklist_projects:
            - beta
          blocklist_accounts:
            - [email protected]
          allowlist_account_types:
            - user
            - group
            - serviceAccount
          blocklist_account_types:
            - None
          min_safe_to_apply_score_user: 0
          min_safe_to_apply_score_group: 0
          min_safe_to_apply_score_SA: 50

    esstore:
      plugin: CureIAM.plugins.elastic.esstore.EsStore
      params:
        # Change http to https later if your elastic are using https
        scheme: http
        host: es-host.com
        port: 9200
        index: cureiam-stg
        username: security
        password: securepassword

Each of these plugins declaration has to be of this form:

  plugins:
    <plugin-name>:
      plugin: <class-name-as-python-path>
      params:
        param1: val1
        param2: val2

For example, for plugins CureIAM.stores.esstore.EsStore which is this file and class EsStore. All the params which are defined in yaml has to match the declaration in __init__() function of the same plugin class.

  1. Once plugins are defined , next step is to define how to define pipeline for auditing. And it goes like this:
  audits:
    IAMAudit:
      clouds:
        - gcpCloud
      processors:
        - gcpIamProcessor
      stores:
        - filestore
        - esstore

Multiple Audits can be created out of this. The one created here is named IAMAudit with three plugins in use, gcpCloud, gcpIamProcessor, filestores and esstore. Note these are the same plugin names defined in Step 2. Again this is like defining the pipeline, not actually running it. It will be considered for running with definition in next step.

  1. Tell CureIAM to run the Audits defined in previous step.
  run:
    - IAMAudits

And this makes the entire configuration for CureIAM, you can find the full sample here, this config driven pipeline concept is inherited from Cloudmarker framework.

Dashboard

The JSON which is indexed in elasticsearch using Elasticsearch store plugin, can be used to generate dashboard in Kibana.

Contribute

[Please do!] We are looking for any kind of contribution to improve CureIAM's core funtionality and documentation. When in doubt, make a PR!

Credits

Gojek Product Security Team ❤️

Demo

<>

=============

NEW UPDATES May 2023 0.2.0

Refactoring

  • Breaking down the large code into multiple small function
  • Moving all plugins into plugins folder: Esstore, files, Cloud and GCP.
  • Adding fixes into zero divide issues
  • Migration to new major version of elastic
  • Change configuration in CureIAM.yaml file
  • Tested in python version 3.9.X

Library Updates

Adding the version in library to avoid any back compatibility issues.

  • Elastic==8.7.0 # previously 7.17.9
  • elasticsearch==8.7.0
  • google-api-python-client==2.86.0
  • PyYAML==6.0
  • schedule==1.2.0
  • rich==13.3.5

Docker Files

  • Adding Docker Compose for local Elastic and Kibana in elastic
  • Adding .env-ex change .env-ex to .env to before running the docker
Running docker compose: docker-compose -f docker_compose_es.yaml up 

Features

  • Adding the capability to run scan without applying the recommendation. By default, if mode_scan is false, mode_enforce won't be running.
      mode_scan: true
      mode_enforce: false
  • Turn off the email function temporarily.

cureiam's People

Contributors

kd3vhck avatar rosehgal 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

Watchers

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

cureiam's Issues

Facing issues while running CureIAM

python3 -m CureIAM -n

2023-01-22 16:23:28 [34581] [MainProcess] [MainThread] INFO CureIAM.manager:36 - CureIAM 0.1.0
2023-01-22 16:23:28 [34581] [MainProcess] [MainThread] INFO CureIAM.util:38 - Looking for /etc/CureIAM.yaml
2023-01-22 16:23:28 [34581] [MainProcess] [MainThread] INFO CureIAM.util:43 - Found /etc/CureIAM.yaml
2023-01-22 16:23:28 [34581] [MainProcess] [MainThread] INFO CureIAM.util:38 - Looking for /root/.CureIAM.yaml
2023-01-22 16:23:28 [34581] [MainProcess] [MainThread] INFO CureIAM.util:38 - Looking for /root/CureIAM.yaml
2023-01-22 16:23:28 [34581] [MainProcess] [MainThread] INFO CureIAM.util:38 - Looking for CureIAM.yaml
2023-01-22 16:23:28 INFO [34581] CureIAM.manager:51 - CureIAM 0.1.0; configured manager.py:51
INFO [34581] CureIAM.manager:56 - Starting job now manager.py:56
INFO [34581] CureIAM.manager:284 - Skipping email notification because email config is missing; about: all audits; state: starting manager.py:284
INFO [34581] CureIAM.manager:284 - Skipping email notification because email config is missing; about: IAMAudit; state: starting manager.py:284
INFO [34582] CureIAM.workers:190 - store_worker: IAMAudit_filestore: Started workers.py:190
INFO [34585] CureIAM.workers:88 - processor_worker: IAMAudit_gcpIamProcessor: Started workers.py:88
INFO [34583] CureIAM.workers:190 - store_worker: IAMAudit_esstore: Started workers.py:190
INFO [34584] CureIAM.workers:32 - cloud_worker: IAMAudit_gcpCloud: Started workers.py:32
ERROR [34585] CureIAM.workers:93 - processor_worker: IAMAudit_gcpIamProcessor: Failed; error: FileNotFoundError: [Errno 2] No such file or directory: 'cureiamSA.json - foo - workers.py:93
bar'
Traceback (most recent call last):
File "/home/test/CureIAM/CureIAM/workers.py", line 91, in processor_worker
plugin = util.load_plugin(plugin_config)
File "/home/test/CureIAM/CureIAM/util.py", line 120, in load_plugin
plugin = plugin_class(**plugin_params)
File "/home/test/CureIAM/CureIAM/processors/gcpcloudiam.py", line 47, in init
self._cloud_resource = util.build_resource(
File "/home/test/CureIAM/CureIAM/util.py", line 643, in build_resource
credential = service_account.Credentials.from_service_account_file(
File "/usr/local/lib/python3.9/dist-packages/google/oauth2/service_account.py", line 241, in from_service_account_file
info, signer = _service_account_info.from_filename(
File "/usr/local/lib/python3.9/dist-packages/google/auth/_service_account_info.py", line 80, in from_filename
with io.open(filename, "r", encoding="utf-8") as json_file:
FileNotFoundError: [Errno 2] No such file or directory: 'cureiamSA.json - foo - bar'
INFO [34585] CureIAM.workers:95 - processor_worker: IAMAudit_gcpIamProcessor: Stopped workers.py:95
ERROR [34584] CureIAM.workers:51 - cloud_worker: IAMAudit_gcpCloud: Failed; error: FileNotFoundError: [Errno 2] No such file or directory: 'cureiamSA.json' workers.py:51
Traceback (most recent call last):
File "/home/test/CureIAM/CureIAM/workers.py", line 35, in cloud_worker
plugin = util.load_plugin(plugin_config)
File "/home/test/CureIAM/CureIAM/util.py", line 120, in load_plugin
plugin = plugin_class(**plugin_params)
File "/home/test/CureIAM/CureIAM/clouds/gcpcloud.py", line 40, in init
credentials = service_account.Credentials.from_service_account_file(
File "/usr/local/lib/python3.9/dist-packages/google/oauth2/service_account.py", line 241, in from_service_account_file
info, signer = _service_account_info.from_filename(
File "/usr/local/lib/python3.9/dist-packages/google/auth/_service_account_info.py", line 80, in from_filename
with io.open(filename, "r", encoding="utf-8") as json_file:
FileNotFoundError: [Errno 2] No such file or directory: 'cureiamSA.json'
INFO [34584] CureIAM.workers:54 - cloud_worker: IAMAudit_gcpCloud: Stopped workers.py:54
INFO [34582] CureIAM.workers:204 - store_worker: IAMAudit_filestore: Stopping workers.py:204
INFO [34582] CureIAM.workers:224 - store_worker: IAMAudit_filestore: Stopped workers.py:224
ERROR [34583] CureIAM.workers:195 - store_worker: IAMAudit_esstore: Failed; error: ImportError: cannot import name 'ElasticsearchException' from 'elasticsearch' workers.py:195
(/usr/local/lib/python3.9/dist-packages/elasticsearch/init.py)
Traceback (most recent call last):
File "/home/test/CureIAM/CureIAM/workers.py", line 193, in _write_worker
plugin = util.load_plugin(plugin_config)
File "/home/test/CureIAM/CureIAM/util.py", line 113, in load_plugin
plugin_module = importlib.import_module(parts[0])
File "/usr/lib/python3.9/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1030, in _gcd_import
File "", line 1007, in _find_and_load
File "", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "", line 790, in exec_module
File "", line 228, in _call_with_frames_removed
File "/home/test/CureIAM/CureIAM/stores/esstore.py", line 8, in
from elasticsearch import Elasticsearch, ElasticsearchException
ImportError: cannot import name 'ElasticsearchException' from 'elasticsearch' (/usr/local/lib/python3.9/dist-packages/elasticsearch/init.py)
INFO [34583] CureIAM.workers:197 - store_worker: IAMAudit_esstore: Stopped workers.py:197
INFO [34581] CureIAM.manager:284 - Skipping email notification because email config is missing; about: IAMAudit; state: ending manager.py:284
INFO [34581] CureIAM.manager:284 - Skipping email notification because email config is missing; about: all audits; state: ending manager.py:284

Please help me with this, did I miss out anything?

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.