Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinc committed Dec 28, 2018
0 parents commit 89f085d
Show file tree
Hide file tree
Showing 34 changed files with 19,356 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
/cache
/dashboard/data
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.5.1
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sudo: false
language: ruby
rvm:
- 2.5.1
before_install: gem install bundler -v 1.16.1
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# Specify your gem's dependencies in rbacvis.gemspec
gemspec
70 changes: 70 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
PATH
remote: .
specs:
rbacvis (0.1.0)
activesupport
colorize
commander
hashie
redisgraph

GEM
remote: https://rubygems.org/
specs:
activesupport (5.2.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
colorize (0.8.1)
commander (4.4.7)
highline (~> 2.0.0)
concurrent-ruby (1.1.4)
diff-lcs (1.3)
hashie (3.6.0)
highline (2.0.0)
i18n (1.3.0)
concurrent-ruby (~> 1.0)
minitest (5.11.3)
power_assert (1.1.3)
rake (10.5.0)
rdoc (6.0.4)
redis (4.1.0)
redisgraph (1.0.0)
redis (~> 4)
terminal-table (~> 1, >= 1.8)
rspec (3.8.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
test-unit (3.2.9)
power_assert
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
unicode-display_width (1.4.1)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.16)
rake (~> 10.0)
rbacvis!
rdoc
rspec (~> 3.0)
test-unit

BUNDLED WITH
1.16.1
8 changes: 8 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Name: rbacvis
Copyright (c) 2018 Marcin Ciszak

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.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# rbacvis - Kubernetes RBAC static analysis & visualisation tool

Author:: Marcin Ciszak ([email protected])

Copyright:: Copyright (c) 2018 Appvia

License:: mit, see LICENSE.txt

## Links

* https://github.com/appvia/rbacvis

## Install

```
./bin/setup
```

## Examples

### Run report

1. To run report against running cluster you must provide kubectl context
```
./bin/rbacvis report -k <context> -c <cluster-name>
```

1. To run report against local RBAC yaml files, provide directory path
```
./bin/rbacvis report -d <directory> -c <cluster-name>
```
NOTE: rbacvis expects the following files to be present (psp.yaml, roles.yaml, clusterroles.yaml, rolebindings.yaml, clusterrolebindings.yaml)

### Dashboard

To see RBAC tree and associated findings run the following command to start UI for given cluster name
```
./bin/rbacvis dashboard -c <cluster-name>
```

## Tests

```
bundle exec rspec
```

## Contributing

14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "rbac_visualiser"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start(__FILE__)
96 changes: 96 additions & 0 deletions bin/rbacvis
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env ruby

require 'rubygems'
require 'bundler'
Bundler.require(:default)

require 'commander'
require 'colorize'
require 'redisgraph'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/string'

require 'rbac_visualiser'
require 'cli'

class CLI
include Commander::Methods
include Cli::Helpers
include RbacVisualiser::Helpers

DEFAULT_DASHBOARD_PORT = 8000
DEFAULT_REPORT_OUTPUT = :yaml

def run
program :name, 'rbacvis'
program :version, RbacVisualiser::VERSION
program :description, 'Kubernetes RBAC static analysis & visualisation tool'
program :help, 'Author', 'Marcin Ciszak <[email protected]>'

# never_trace!

default_command :help

command :report do |c|
c.syntax = 'rbacvis report [options]'
c.summary = 'K8s RBAC report'
c.description = 'Generates K8s RBAC static analysis report for given cluster'
c.example 'to generate report', 'rbacvis report --cluster your-cluster-name'
c.option '-c', '--cluster [CLUSTER NAME]', String, 'Cluster name'
c.option '-o', '--output [OUTPUT]', String, '(json|yaml|none) Default: yaml'
c.option '-d', '--dir [PATH]', String, 'Path to RBAC yaml files (roles, rolebinding, clusterroles, clusterrolebinding, psps)'
c.option '-k', '--kubectlcontext [CONTEXT]', String, 'Kubectl config context name for fetching RBAC from running cluster. This is alternative to loading RBAC from the filesystem.'
c.option '--verbose', 'Makes output verbose'
c.action do |args, options|
options.default output: DEFAULT_REPORT_OUTPUT
options.default dir: nil
options.default kubectlcontext: nil
options.default local: false

banner :info, 'This may take several seconds to complete...'

raise_on_cluster_missing options
raise_on_missing_path_or_context options

report = RbacVisualiser::Report.new(
cluster: options.cluster,
dir: options.dir,
kubectlcontext: options.kubectlcontext,
verbose: !!options.verbose,
index: true
).run

say report.to_yaml if options.output.to_sym == :yaml
say JSON.pretty_generate(report) if options.output.to_sym == :json
end
end

command :dashboard do |c|
c.syntax = 'rbacvis dashboard [options]'
c.summary = 'K8s RBAC Visualisation dashboard'
c.description = 'Kubernetes RBAC Visualisation dashboard'
c.example 'to open UI dashboard', 'rbacvis dashboard'
c.option '-c', '--cluster [CLUSTER NAME]', String, 'Cluster name'
c.option '-p', '--port [PORT]', Integer, 'Dahboard UI custom port. Default: 8000'
c.option '--verbose', 'Makes output verbose'
c.action do |args, options|
options.default port: DEFAULT_DASHBOARD_PORT

raise_on_cluster_missing options
raise_on_cluster_report_missing options

say "---".light_cyan
say "RBAC visualiser DASHBOARD:".light_cyan
say "http://127.0.0.1:#{options.port}/dashboard?cluster=#{options.cluster}".light_cyan
say "---".light_cyan

%x(ruby -run -e httpd . -p #{options.port})
end
end

run!
end

end

CLI.new.run if $0 == __FILE__
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
103 changes: 103 additions & 0 deletions config/rules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
rules:
- group_title: "Subjects with privileged PSP access not scoped to a namespace"
severity: :danger
info: "Subjects below are able to run privileged containers and are not scoped to a particular namespace. This might be potential security risk!"
query: |
MATCH (p:Psp)-[:SECURITY]->(r:Rule)<-[:GRANT]-(ro:Role)<-[:ASSIGN]-(s:Subject)
WHERE p.privileged='true' AND p.name != 'kube-system' AND (s.namespace = '' OR s.namespace = NULL) AND ro.defined = true
RETURN p.name as psp_name, s.kind as subject_kind, s.name as subject_name
writer: |
"psp: #{r[:psp_name]} - #{r[:subject_kind]} #{r[:subject_name]}"
- group_title: "Subjects with open cluster-wide access"
severity: :warning
info: "Subjects with * access to * resources (apiGroups: *, core) in the context of an entire cluster. This might be potential security risk!"
query: |
MATCH (r:Rule)<-[:GRANT]-(ro:Role)<-[:ASSIGN]-(s:Subject)
WHERE ((r.api_group = '*' OR r.api_group = 'core') AND (r.resource = '*' OR r.url = '*') AND r.verb = '*')
AND (s.namespace = '' OR s.namespace = NULL) AND ro.defined = true
RETURN r.api_group as rule_api_group, r.type as rule_type, r.resource as rule_resource, r.url as rule_url, r.verb as rule_verb,
s.kind as subject_kind, s.name as subject_name
ORDER BY subject_name,subject_kind
writer: |
txt = r[:rule_type] == 'resource' ? 'resources' : 'non-resource URLs'
"#{r[:subject_kind]} #{r[:subject_name]} has * access to * #{txt} (apiGroup: #{r[:rule_api_group]})"
- group_title: "Subjects with open namespace level access"
severity: :warning
info: "Subjects with * access to * resources (apiGroups: *, core) within the scope of a namespace. This might be potential security risk."
query: |
MATCH (r:Rule)<-[:GRANT]-(ro:Role)<-[:ASSIGN]-(s:Subject)
WHERE ((r.api_group = '*' OR r.api_group = 'core') AND (r.resource = '*' OR r.url = '*') AND r.verb = '*')
AND s.namespace != '' AND ro.defined = true
RETURN r.api_group as rule_api_group, r.type as rule_type, r.resource as rule_resource, r.url as rule_url, r.verb as rule_verb,
s.kind as subject_kind, s.name as subject_name, s.namespace as subject_namespace
ORDER BY subject_namespace, subject_name, subject_kind
writer: |
txt = r[:rule_type] == 'resource' ? 'resources' : 'non-resource URLs'
"#{r[:subject_kind]} #{r[:subject_name]} has * access to * #{txt} (apiGroup: #{r[:rule_api_group]}) in #{r[:subject_namespace]} namespace"
- group_title: "Subjects with ability to manage RBAC"
severity: :warning
info: "List of Subjects who can manage Roles and RoleBindings. This might be potential security risk."
query: |
MATCH (s:Subject)-[:ASSIGN]->(ro:Role)-[:GRANT]->(r:Rule)
WHERE ro.defined = true
AND (r.verb = 'create' OR r.verb = '*')
AND (r.api_group = 'rbac.authorization.k8s.io' OR r.api_group = '*')
AND (r.resource = 'roles' OR r.resource = 'rolebindings' OR r.resource = '*')
RETURN s.kind as subject_kind, s.name as subject_name, s.namespace as subject_namespace
ORDER BY subject_kind, subject_name DESC
writer: |
ns = r[:subject_namespace].blank? ? 'Cluster-Wide' : "in #{r[:subject_namespace]} namespace"
"#{r[:subject_kind]} #{r[:subject_name]} (#{ns})"
- group_title: "Subjects associated with non-existing roles"
severity: :warning
info: "List of Subjects with Role or ClusterRole which hasn't been defined. Check whether this Role should exist."
query: |
MATCH (s:Subject)-[:ASSIGN]->(r:Role)
WHERE r.defined = false
RETURN r.kind as role_kind, r.name as role_name, s.kind as subject_kind,
s.name as subject_name, s.namespace as subject_namespace
ORDER BY subject_namespace, subject_name, subject_kind
writer: |
"#{r[:subject_kind]} #{r[:subject_name]} is bound to non-existing #{r[:role_kind]} #{r[:role_name]}"
- group_title: "Dangling roles (not bound to any subject)"
severity: info
info: "List of roles that are defined however not assigned to any of the Subjects. Is this Role/ClusteRole needed?"
query: |
MATCH (r:Role)-[:ASSIGN]->(s:Subject)
WHERE r.defined = true AND s.name = NULL
RETURN r.kind as role_kind, r.name as role_name
ORDER BY role_kind, role_name
writer: |
"#{r[:role_kind]} #{r[:role_name]} is not bound to any Subject(s)"
- group_title: "Subjects associated with multiple roles"
severity: :info
info: "List of Subjects associated with multiple roles. This may indicate a bad RBAC design. Consider role aggregation."
query: |
MATCH (s:Subject)-[:ASSIGN]->(r:Role)
WHERE r.defined = true
RETURN count(r) as role_count, s.kind as subject_kind, s.name as subject_name, s.namespace as subject_namespace
ORDER BY role_count, subject_namespace, subject_name, subject_kind DESC
threshold: 4
writer: |
"#{r[:subject_kind]} #{r[:subject_name]} referenced by #{r[:role_count].to_i} roles" if r[:role_count].to_i >= threshold
- group_title: "Namespaces with multiple subjects"
severity: :info
info: |
List of Namespaces with multiple subjects. Sagnificant number of Subjects having access
to Namespace might indicate bad design or potential security risk.
query: |
MATCH (s:Subject)
RETURN count(s.name) as subject_count, s.namespace as subject_namespace
ORDER BY subject_count, subject_namespace DESC
threshold: 5
writer: |
ns = r[:subject_namespace].empty? ? 'CLUSTER-WIDE' : "#{r[:subject_namespace]} namespace"
"#{ns} allows #{r[:subject_count].to_i} subject(s)" if r[:subject_count].to_i >= threshold
Loading

0 comments on commit 89f085d

Please sign in to comment.