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

Optional API token-based authentication #8

Merged
merged 21 commits into from
Dec 5, 2017
Merged

Optional API token-based authentication #8

merged 21 commits into from
Dec 5, 2017

Conversation

rthomson
Copy link

This pull request implements optional API token-based authentication when netbox is configured with LOGIN_REQUIRED = True in configuration.py.

netbox.yml example:

netbox:
    main:
        api_url: 'https://localhost/api/dcim/devices/'
        api_token: '1234567890987654321234567890987654321'

where the value of api_token is equivalent to any valid netbox user profile API token.

The api_token key can be omitted from netbox.yml and netbox-as-ansible-inventory behaves as expected (no change to previous behavior).

@rthomson
Copy link
Author

Seems there are some tests to update for this PR to pass. Looking into it.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.5%) to 97.692% when pulling d72878e on rthomson:master into f88b5f2 on AAbouZaid:master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-1.5%) to 97.692% when pulling d72878e on rthomson:master into f88b5f2 on AAbouZaid:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.5%) to 97.692% when pulling d72878e on rthomson:master into f88b5f2 on AAbouZaid:master.

@rthomson
Copy link
Author

Got the CI tests to pass by updating tests/test_netbox.py.

I'm not yet clear how on how update the tests so that coveralls passes for key_value = "" and api_url_headers = {}.

I'm new to tests to please bear with me.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.8%) to 98.4% when pulling 13302ae on rthomson:master into f88b5f2 on AAbouZaid:master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.8%) to 98.4% when pulling 13302ae on rthomson:master into f88b5f2 on AAbouZaid:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.8%) to 98.4% when pulling 13302ae on rthomson:master into f88b5f2 on AAbouZaid:master.

@coveralls
Copy link

coveralls commented Nov 22, 2017

Coverage Status

Coverage decreased (-0.8%) to 98.4% when pulling f51e62f on rthomson:master into f88b5f2 on AAbouZaid:master.

@rthomson
Copy link
Author

I'm admitting defeat on the final coveralls test. I'm not sure I understand how coveralls works nor how to write tests that will cover the case that coveralls is still complaining about.

@coveralls
Copy link

coveralls commented Nov 22, 2017

Coverage Status

Coverage remained the same at 99.2% when pulling 62bc71e on rthomson:master into f88b5f2 on AAbouZaid:master.

@rthomson
Copy link
Author

💯 Finally!

@aabouzaid
Copy link
Owner

aabouzaid commented Nov 22, 2017

@rthomson Thanks a lot, that looks pretty nice!
Could you please switch it develop branch instead master, then I will review and merge.

@rthomson rthomson changed the base branch from master to devel November 22, 2017 21:40
@rthomson
Copy link
Author

@aabouzaid done!

@aabouzaid
Copy link
Owner

Cool, I'm doing a quick review now 👍

netbox/netbox.py Outdated
@@ -79,6 +79,7 @@ def __init__(self, script_args, script_config_data):
# Script configuration.
self.script_config = script_config_data
self.api_url = self._config(["main", "api_url"])
self.api_token = self._config(["main", "api_token"], ignore_key_error=True)
Copy link
Owner

@aabouzaid aabouzaid Nov 22, 2017

Choose a reason for hiding this comment

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

Why ignore_key_error? This should be default=None instead ignoring key error.

Copy link
Author

Choose a reason for hiding this comment

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

Letting the default value be assigned when api_token was missing from netbox.yml was resulting a sys.exit() in _config() due to the if value: conditional evaluation on line 144-148 (that I removed/changed).

Looking into it.

netbox/netbox.py Outdated
"""Get value from config var.

Args:
key_path: A list has the path of the key.
default: Default value if the key is not found.

Returns:
The value of the key from config file or the default value.
The value of the key from config file or the default value
Copy link
Owner

Choose a reason for hiding this comment

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

The dot? :-)

Copy link
Author

Choose a reason for hiding this comment

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

I don't even remember that! Typo.

netbox/netbox.py Outdated
key_value = value
else:
sys.exit("The key '%s' is not found in config file." % ".".join(key_path))
key_value = self._get_value_by_path(config, key_path, ignore_key_error=ignore_key_error, default=default)
Copy link
Owner

Choose a reason for hiding this comment

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

I kept that part like that because I like to have a different message than the default one in _get_value_by_path (I found it a bit misleading, that's why I use a different error message here).
So I'd suggest to edit _get_value_by_path to have the ability to change the error message.

Copy link
Author

@rthomson rthomson Nov 22, 2017

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 would result in sys.exit() if a default value of "" was assigned to api_token when missing from netbox.yml.

I'm reviewing the logic to see if there's a simpler/better way to have an optional key for api_token in netbox.yml.

@@ -34,6 +34,7 @@
netbox:
main:
api_url: 'http://localhost/api/dcim/devices/'
api_token: '1234567890987654321234567890987654321'
Copy link
Owner

@aabouzaid aabouzaid Nov 22, 2017

Choose a reason for hiding this comment

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

Let's have a commented key inside netbox.yaml to make it part of conf file.
e.g.

netbox:
     main:
         api_url: 'http://localhost/api/dcim/devices/'
         #api_token: ''

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good.

Copy link
Owner

@aabouzaid aabouzaid left a comment

Choose a reason for hiding this comment

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

Some parts need a second look?
Thanks a lot for your effort :-)

@coveralls
Copy link

coveralls commented Nov 22, 2017

Coverage Status

Coverage remained the same at 99.2% when pulling f25cf5f on rthomson:master into 8607dd5 on AAbouZaid:devel.

@rthomson
Copy link
Author

rthomson commented Nov 22, 2017

I made some changes to remove the stuff around ignore_key_error that I added. It now relies on the default value without going to sys.exit() if api_token isn't found. I did have to add a conditional to __init__ for NetboxAsInventory to sys.exit() if config was empty to satisfy a test (that I believe was satisfied by the previous logic that was sys.exit()ing when api_token wasn't found).

Not sure if this is any better or not. Also, I'm fumbling my way though pull requests, CI tests and code review as there are all new for me! I appreciate your pointing out when/how components of this PR require improvement.

@coveralls
Copy link

coveralls commented Nov 23, 2017

Coverage Status

Coverage increased (+0.01%) to 99.213% when pulling df6c248 on rthomson:master into 8607dd5 on AAbouZaid:devel.

@aabouzaid
Copy link
Owner

aabouzaid commented Nov 23, 2017

You actually are doing good :-)
I'd suggest to have a boolean arg in _config, e.g. optional=Truel, so it passed that value to _get_value_by_path to ignore the error.
That should be fine.

And in all cases, since there a new behavior has been introduced, it's fine to change the tests to fit that case.

@aabouzaid
Copy link
Owner

Actually now I'm thinking about another way to deal with config in general.

@aabouzaid
Copy link
Owner

@rthomson I did the fix to make any conf key optional, later I will find a better way to handle the config.

Could you please pull the branch optional_key and rebase it on yours then push it to merge.

Thanks.

@rthomson
Copy link
Author

Could you please pull the branch optional_key and rebase it on yours then push it to merge.

I will take a look this week!

Thank you.

@coveralls
Copy link

coveralls commented Dec 5, 2017

Coverage Status

Coverage increased (+0.03%) to 99.231% when pulling f58fa3a on rthomson:master into 8607dd5 on AAbouZaid:devel.

@rthomson
Copy link
Author

rthomson commented Dec 5, 2017

Pulling a new branch from upstream and rebasing against my changes, including manual merge conflict resolution, was an interesting learning experience!

I'm not sure but I think it might have worked...

@aabouzaid
Copy link
Owner

Well, now we have duplication of commits (the rebase should be on my last comment only).
So you need to do something like that to fix it.

On your branch.

git checkout master
git checkout c646552
git push origin master -f

I think it's a nice experience in all cases 😁

@rthomson
Copy link
Author

rthomson commented Dec 5, 2017

Well, now we have duplication of commits (the rebase should be on my last comment only).

Oops.

I think it's a nice experience in all cases 😁

Absolutely! And thanks again for your patience.

I'll keep working at it.

@aabouzaid aabouzaid merged commit 9c97a1d into aabouzaid:devel Dec 5, 2017
@aabouzaid
Copy link
Owner

It has been merged 🎉
It had some extra commits but I did rebase it on my side to avoid more toing and froing :-)

Thanks a lot for your effort @rthomson, keep going you are doing well 🤘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants