Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

LOG-9289 apikeys and live tail supports #42

Merged
merged 6 commits into from
May 24, 2017
Merged

Conversation

stopal-r7
Copy link
Contributor

@stopal-r7 stopal-r7 commented May 23, 2017

  • 1st commit: Introduced apikeys and live tail supports to lecli

  • 2nd commit: (No functional change here) There has been a major cleanup in UTs such that I have removed dummy urls and response examples that was not helping to test lecli functionality. Only left the ones that helps to verify lecli implementation

  • 3rd commit: (Only functional change is around config file in this commit) I have tried to simplify and standardise url building logic among different api implementations. I have also removed management and query URLs from config file and introduced "api url" which is supposed to be the base url for REST API

  • 4th commit: Owner support for listing api keys to show the owner api key itself and documentation for new APIs(live tail and api key management)


def create(payload):
"""
Delete an api key with the provided ID
Copy link
Contributor

Choose a reason for hiding this comment

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

Think this was a copy-paste error!

sys.stderr.write('Create api key failed, status code: %d' % response.status_code)
sys.exit(1)
elif response.status_code == 201:
sys.stdout.write('Created api key with payload: %s\n' % payload)
Copy link
Contributor

Choose a reason for hiding this comment

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

Are the response body & the payload here identical?

If not, we should probably print out the body.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we are printing the response body on the next line. I will remove printing the payload though, I think I put this in for debugging purposes. :)

Handle get api key response
"""
if response_utils.response_error(response):
sys.exit(1)

Choose a reason for hiding this comment

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

Not printing out an error message here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

response_utils does

try:
response = requests.delete(url, headers=headers)
if response_utils.response_error(response):
sys.stderr.write('Deleting api key failed, status code: %d' % response.status_code)

Choose a reason for hiding this comment

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

How come we're printing this here? Does response_utils not print the status code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nice catch, response_utils does print the status code so I will remove it from here.

sys.stderr.write('Deleting api key failed, status code: %d' % response.status_code)
sys.exit(1)
elif response.status_code == 204:
sys.stdout.write('Deleted api key with id: %s \n' % api_key_id)

Choose a reason for hiding this comment

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

What happens when this branch is false, and it's not a 204? If there's no possibility for another status code we can just have an else here and maybe a comment to say the status is 204

Choose a reason for hiding this comment

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

Same can be applied to all of the functions here

response = requests.get(url, headers=headers)
handle_api_key_response(response)
except requests.exceptions.RequestException as error:
sys.stderr.write(error)

Choose a reason for hiding this comment

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

These logging statements can be refactored to use the logging module built into python.
Might take a bit of time, so not a blocker.

Much nicer to have log.error(error) 🙂

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 was done in this way for a reason -- possibly a feedback from a reviewer before. @ellynch-r7 do you remember why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actualy a better way of doing this is with click.echo(err=True) as logging will require adopting logging for this as well + there are not really logs but information that has to be printed on screen.

I have used click.echo for team api, hopefully in the future we can use it all around the project. For the moment I have reached my refactoring limits :)

Choose a reason for hiding this comment

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

Does click.echo provide the option to add logging handlers? i.e. directing it to a file or a webhook? With python logging module we can just add le_python as a dependency and provide a log token

Copy link
Contributor Author

@stopal-r7 stopal-r7 May 24, 2017

Choose a reason for hiding this comment

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

to a file yes not sure about logging handlers.

with open(filename) as json_data:
try:
params = json.load(json_data)
print params

Choose a reason for hiding this comment

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

print statement here

params = json.load(json_data)
print params
api.create(params)
except ValueError as error:

Choose a reason for hiding this comment

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

This should only be wrapped around the one line, params = json.load(json_data) right?
I don't believe api.create will throw that exception, or will it?

else:
print_config_error_and_exit(URL_SECTION, 'Log Query URL(%s)' % config_key)
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
return 'https://rest.logentries.com/query'

Choose a reason for hiding this comment

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

This should be extracted as a constant. So it's easier to modify later. As well as the other default urls in the file

@cli.group()
def tail():
"""Tail logs"""
pass

Choose a reason for hiding this comment

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

Should we bother committing this if it's incomplete?

Copy link
Contributor Author

@stopal-r7 stopal-r7 May 24, 2017

Choose a reason for hiding this comment

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

this isn't incomplete actually, its there for registering that group with click context

Choose a reason for hiding this comment

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

Ah ok

next_url = response.json()['links'][0]['href']
time.sleep(poll_interval)
response = fetch_results(next_url)

Choose a reason for hiding this comment

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

What happens here if there is no links field in the response?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there has to be a link in the response as its live tail

Choose a reason for hiding this comment

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

Then should we print an error? It will just end silently if something weird comes back

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do

api.list()

out, err = capsys.readouterr()
assert not err

Choose a reason for hiding this comment

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

Can we check that something has been printed to out?
If someone creates a branch with no error output, it will still pass


out, err = capsys.readouterr()
assert not err
assert 'Disabled api key with id: %s' % api_key_id in out

Choose a reason for hiding this comment

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

There's no guarantee from this that we've sent the correct body. Can we check what calls are made to the mocked http client?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah can check that i guess with httpretty.last_request 👍

@stopal-r7 stopal-r7 merged commit 9d925e4 into rapid7:master May 24, 2017
@stopal-r7 stopal-r7 deleted the LOG-9289 branch May 30, 2017 12:19
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants