Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate eso-client to nexpose-client / CSRF header update #309

Merged
merged 19 commits into from
Feb 4, 2020

Conversation

hdub-tech
Copy link
Contributor

PLEASE DO NOT NITPICK THIS CODE. ITS A COPY/PASTE AND NOT NEW DEVELOPMENT

Description

This is literally a copy/paste of the eso-client gem into nexpose-client. Is it beautiful? No. I wanted to refactor a bunch of things here before we did this. But I don't know when I will have the excuse to be in this code base in the near future.

Motivation and Context

When this is merged, users will be able to do the following:

  • Create and manipulate "new style" discovery connections and their options which do asset syncing (AWS, EPO, Azure, DXL)
  • Create and manipulate Automated Actions
  • It also fixes calls to the newly CSRF protected ESO endpoints

How Has This Been Tested?

I haven't tested it yet. I ran one test locally and it worked. I want a pipeline to run. Actually I want several to run. So don't merge it.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist:

  • All new and existing tests passed.
  • A nexpose pipeline ran using it (Don't paste link)
  • Get rid of eso/version.rb
  • Bump nexpose gem version number
  • Ensure this works for gem release process

Need issues for the following, as I won't be able to do them as part of this PR:

  • Refactor service.rb to use Nexpose::AJAX ( I started this and kinda gave up cuz its a massive undertaking, particularly because of the affect on the tests for the Nexpose repo. I'll link the branch.)
  • Refactor to get rid of ESO module and strictly make Nexpose.
  • I have updated the wiki accordingly .
  • I have added tests to cover my changes.

# @return [Step] Step object corresponding to the given service.
#
def get_step(type_name)
@steps.find do |step|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer detect over find.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@id = id
@name = name
@steps = steps
@timeCreated = timeCreated

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use snake_case for variable names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trust me @houndci-bot I want to. But there is some old code here, notably to_hash, which is kinda cheating in its parsing and directly converting the JSON from Nexpose (which uses camelCase) directly into the variables for this class. And if I change that, I'd have to refactor a wholllllleeeee lotta code.

@rapid7 rapid7 deleted a comment from houndci-bot Dec 12, 2017
fruechel added a commit to asecurityteam/nexpose-client-python that referenced this pull request Dec 14, 2017
This mirrors the changes in
rapid7/nexpose-client#309 but limited to
discovery connections.
@rapid7 rapid7 deleted a comment from houndci-bot Dec 14, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Dec 15, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Dec 15, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Dec 15, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Dec 15, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Dec 15, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Dec 15, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Dec 15, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Dec 15, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Dec 15, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Dec 15, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Dec 15, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Dec 15, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Dec 15, 2017
STOPPED = 'stopped'

# Workflow or an integration option has experienced an error that caused it to stop
ERROR = 'error'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freeze mutable objects assigned to constants.

RETRY = 'retry'

# Workflow or an integration option is stopped by the user
STOPPED = 'stopped'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freeze mutable objects assigned to constants.

RUNNING = 'running'

# The workflow or an integration option is running, but is temporarily unsuccessful processing events
RETRY = 'retry'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freeze mutable objects assigned to constants.

READY = 'ready'

# Workflow or an integration option is processing or has processed events
RUNNING = 'running'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freeze mutable objects assigned to constants.

# Representation of state of a workflow or integration option. Taken from service-orchestration State.java
module State
# Workflow or an integration option is configured and ready to accept events
READY = 'ready'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freeze mutable objects assigned to constants.

hash = {
:typeName => @typeName,
:previousTypeName => @previousTypeName,
:configurationParams => @configurationParams

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the new Ruby 1.9 hash syntax.

def to_h
hash = {
:typeName => @typeName,
:previousTypeName => @previousTypeName,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the new Ruby 1.9 hash syntax.


def to_h
hash = {
:typeName => @typeName,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.
Use the new Ruby 1.9 hash syntax.

}
else
raise ArgumentError, "Invalid StepConfiguration ConfigurationParameter Property name: #{name}. " +
'Should be one of StepConfiguration::ConfigParamProperties'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align the operands of an expression in an assignment spanning multiple lines.

value: value
}
else
raise ArgumentError, "Invalid StepConfiguration ConfigurationParameter Property name: #{name}. " +

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use \ instead of + or << to concatenate those strings.

# @return [Hash] Hash interpretation of this IntegrationOption.
def to_hash
hash = {}
instance_variables.each {|var| hash[var.to_s.delete("@")] = instance_variable_get(var)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space between { and | missing.
Prefer single-quoted strings when you don't need string interpolation or special symbols.
Space missing inside }.

# Grab the Step objects and convert to Hashes
steps = hash['steps']
hashified_steps = []
steps.each {|step| hashified_steps << step.to_hash}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space between { and | missing.
Space missing inside }.

module IntegrationOptionTypes
# The IMPORT_TO_SITE Array tracks Integration Options which load Assets into a Site.
IMPORT_TO_SITE = [
IntegrationOptionNames::IMPORT_AD_ASSETS,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces for indentation in an array, relative to the start of the line where the left square bracket is.

# IntegrationOptionTypes is a way to categorize what various Integration Options do.
module IntegrationOptionTypes
# The IMPORT_TO_SITE Array tracks Integration Options which load Assets into a Site.
IMPORT_TO_SITE = [

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freeze mutable objects assigned to constants.

IMPORT_AD_ASSETS = 'import_ad_assets'
IMPORT_EPO_ASSETS = 'import_epo_assets'
SYNC_AZURE_ASSETS = 'sync_azure_assets'
SYNC_AZURE_ASSETS_WITH_TAGS = 'sync_azure_assets_with_tags'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Freeze mutable objects assigned to constants.

def self.build_publish_vulnerabilities_option(name:, discovery_conn_id:)
step1 = Step.new(service_name: ServiceNames::NEXPOSE, type_name: StepNames::NEW_ASSET_VULN)
step2 = Step.new(service_name: ServiceNames::DXL, type_name: StepNames::PUBLISH_VULN_INT_TYPE, previous_type_name: step1.type_name)
.add_property(StepConfiguration::ConfigParamProperties::DISCOVERY_CONFIG_ID, discovery_conn_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place the . on the previous line, together with the method call receiver.

.add_property(StepConfiguration::ConfigParamProperties::DISCOVERY_CONFIG_ID, discovery_conn_id)
step2 = Step.new(service_name: ServiceNames::NEXPOSE, type_name: StepNames::VULN_DETAILS, previous_type_name: step1.type_name)
step3 = Step.new(service_name: ServiceNames::DXL, type_name: StepNames::VULN_DETAILS_REQUEST, previous_type_name: step2.type_name)
.add_property(StepConfiguration::ConfigParamProperties::DISCOVERY_CONFIG_ID, discovery_conn_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place the . on the previous line, together with the method call receiver.


def self.build_find_vuln_details_option(name:, discovery_conn_id:)
step1 = Step.new(service_name: ServiceNames::DXL, type_name: StepNames::VULN_DETAILS_REQUEST)
.add_property(StepConfiguration::ConfigParamProperties::DISCOVERY_CONFIG_ID, discovery_conn_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place the . on the previous line, together with the method call receiver.

def self.build_export_risk_scores_option(name:, discovery_conn_id:)
step1 = Step.new(service_name: ServiceNames::NEXPOSE, type_name: StepNames::RISK_SCORE_UPDATED)
step2 = Step.new(service_name: ServiceNames::EPO, type_name: StepNames::PUSH_RISK_SCORE, previous_type_name: step1.type_name)
.add_property(StepConfiguration::ConfigParamProperties::DISCOVERY_CONFIG_ID, discovery_conn_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place the . on the previous line, together with the method call receiver.

.add_property(StepConfiguration::ConfigParamProperties::ONLY_IMPORT_THESE_TAGS, tags)
step2 = Step.new(service_name: ServiceNames::NEXPOSE, type_name: StepNames::SYNC_EXTERNAL, previous_type_name: step1.type_name)

#This isn't always known immediately, which is why we have IntegrationOption.site_id=

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after #.

.add_property(StepConfiguration::ConfigParamProperties::DISCOVERY_CONFIG_ID, discovery_conn_id)
step2 = Step.new(service_name: ServiceNames::NEXPOSE, type_name: StepNames::SYNC_EXTERNAL, previous_type_name: step1.type_name)

#This isn't always known immediately, which is why we have IntegrationOption.site_id=

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after #.


def self.build_sync_aws_assets_option(name:, discovery_conn_id:, site_id: nil)
step1 = Step.new(service_name: ServiceNames::AWS, type_name: StepNames::DISCOVER_AWS_ASSETS)
.add_property(StepConfiguration::ConfigParamProperties::DISCOVERY_CONFIG_ID, discovery_conn_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place the . on the previous line, together with the method call receiver.

.add_property(StepConfiguration::ConfigParamProperties::DISCOVERY_CONFIG_ID, discovery_conn_id)
step2 = Step.new(service_name: ServiceNames::NEXPOSE, type_name: StepNames::IMPORT_EXTERNAL, previous_type_name: step1.type_name)

#This isn't always known immediately, which is why we have IntegrationOption.site_id=

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after #.


def self.build_import_ad_assets_option(name:, discovery_conn_id:, site_id: nil)
step1 = Step.new(service_name: ServiceNames::ACTIVE_DIRECTORY, type_name: StepNames::DISCOVER_ACTIVE_DIRECTORY)
.add_property(StepConfiguration::ConfigParamProperties::DISCOVERY_CONFIG_ID, discovery_conn_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place the . on the previous line, together with the method call receiver.

.add_property(StepConfiguration::ConfigParamProperties::DISCOVERY_CONFIG_ID, discovery_conn_id)
step2 = Step.new(service_name: ServiceNames::NEXPOSE, type_name: StepNames::IMPORT_EXTERNAL, previous_type_name: step1.type_name)

#This isn't always known immediately, which is why we have IntegrationOption.site_id=

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after #.

}
when *ConfigParamPropertyTypes::STRING
{
valueClass: Values::STRING,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

valueClass: Values::INTEGER,
value: value
}
when *ConfigParamPropertyTypes::STRING

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent when as deep as case.

STOPPED = 'stopped'

# Workflow or an integration option has experienced an error that caused it to stop
ERROR = 'error'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/MutableConstant: Freeze mutable objects assigned to constants.

RETRY = 'retry'

# Workflow or an integration option is stopped by the user
STOPPED = 'stopped'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/MutableConstant: Freeze mutable objects assigned to constants.

RUNNING = 'running'

# The workflow or an integration option is running, but is temporarily unsuccessful processing events
RETRY = 'retry'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/MutableConstant: Freeze mutable objects assigned to constants.

READY = 'ready'

# Workflow or an integration option is processing or has processed events
RUNNING = 'running'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/MutableConstant: Freeze mutable objects assigned to constants.

# Representation of state of a workflow or integration option. Taken from service-orchestration State.java
module State
# Workflow or an integration option is configured and ready to accept events
READY = 'ready'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/MutableConstant: Freeze mutable objects assigned to constants.


def to_h
hash = {
:typeName => @typeName,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentHash: Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.
Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

}
else
raise ArgumentError, "Invalid StepConfiguration ConfigurationParameter Property name: #{name}. " +
'Should be one of StepConfiguration::ConfigParamProperties'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/MultilineOperationIndentation: Align the operands of an expression in an assignment spanning multiple lines.

value: value
}
else
raise ArgumentError, "Invalid StepConfiguration ConfigurationParameter Property name: #{name}. " +

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/LineEndConcatenation: Use \ instead of + or << to concatenate those strings.

}
when *ConfigParamPropertyTypes::STRING
{
valueClass: Values::STRING,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentHash: Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

valueClass: Values::INTEGER,
value: value
}
when *ConfigParamPropertyTypes::STRING

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/CaseIndentation: Indent when as deep as case.

@@ -88,7 +88,14 @@ def to_hash
prop = @property.to_sym
hash = {prop => {}}
hash[prop]['valueClass'] = @value_class
hash[prop]['value'] = @value
if @value_class == Eso::Values::ARRAY
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the part that was broken when Nexpose cukes tried to use it.

rv = {}
self.properties.each_pair do |key, value|
if value[:properties]
rv[key] = value if value[:properties].has_key?(:operators)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/PreferredHashMethods: Use Hash#key? instead of Hash#has_key?.

def initialize(uuid: nil, service_name:, workflow: nil, type_name:, previous_type_name: StepNames::EMPTY, configuration_params: nil)
@uuid = uuid if uuid
@serviceName = service_name
@stepConfiguration = StepConfiguration.new(type_name, previous_type_name)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.

#
def initialize(uuid: nil, service_name:, workflow: nil, type_name:, previous_type_name: StepNames::EMPTY, configuration_params: nil)
@uuid = uuid if uuid
@serviceName = service_name

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/VariableName: Use snake_case for variable names.

@@ -0,0 +1,166 @@
module Eso

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true.

}
when *ConfigParamPropertyTypes::INTEGER
{
valueClass: Values::INTEGER,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentHash: Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

valueClass: Values::BOOLEAN,
value: value
}
when *ConfigParamPropertyTypes::INTEGER

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/CaseIndentation: Indent when as deep as case.

case name
when *ConfigParamPropertyTypes::BOOLEAN
{
valueClass: Values::BOOLEAN,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentHash: Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.

def add_property(name, value)
@configurationParams[:properties][name] =
case name
when *ConfigParamPropertyTypes::BOOLEAN

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/CaseIndentation: Indent when as deep as case.

# @return [StepConfiguration] Returns this object for chaining.
def add_property(name, value)
@configurationParams[:properties][name] =
case name

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentAssignment: Indent the first line of the right-hand-side of a multi-line assignment.

@smorris-r7 smorris-r7 merged commit 95479fe into master Feb 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants