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

Add prompting mechanism for config values and add global options: --config, --server, --username, --password, --debug #98

Merged
merged 5 commits into from
Jul 19, 2016

Conversation

msabramo
Copy link
Contributor

@msabramo msabramo commented Jul 15, 2016

This allows not keeping passwords and such in the config file.

E.g.: In /etc/aptly-cli.conf:

---
:proto: http
:server: 10.3.0.46
:port: 8082
:debug: false
:username: ${PROMPT}
:password: ${PROMPT_PASSWORD}

The tool will prompt for the specified values, where ${PROMPT} results in a regular prompt and ${PROMPT_PASSWORD} results in a password prompt where the input is replaced by asterisks -- e.g.:

$ aptly-cli version
Enter a value for username:
zane
Enter a value for password:
***************
{"Version"=>"0.9.7"}

This also eliminates a bunch of duplicated code in all of the command classes by having them inherit from a common base class. This was actually essential because otherwise, every required file would trigger a config load and thus prompting for any needed values over and over again.

The --config (-c) option allows specifying an alternative config file -- e.g.:

$ aptly-cli -c ~/.config/aptly-cli/aptly-cli.conf repo_list

and the --server, --username, and --password options allow specifying those things on the command-line and not even requiring a config file:

$ aptly-cli --server 10.3.0.46 --username marca --password '${PROMPT_PASSWORD}' repo_list

Note that you can use ${PROMPT} and ${PROMPT_PASSWORD} in the values of these options, just as you can in a config file.

elsif v == '${PROMPT_PASSWORD}'
@config[k.to_sym] = password("Enter a value for #{k}:")
else
@config[k.to_sym] = v if @valid_config_keys.include? k.to_sym

Choose a reason for hiding this comment

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

Convert if nested inside else to elsif.

@coveralls
Copy link

coveralls commented Jul 15, 2016

Coverage Status

Coverage decreased (-55.4%) to 36.97% when pulling b03706c on msabramo:password_ask into 616a7d7 on sepulworld:master.

@msabramo msabramo force-pushed the password_ask branch 2 times, most recently from 747dc8a to e3cfb1a Compare July 15, 2016 15:07
@coveralls
Copy link

coveralls commented Jul 15, 2016

Coverage Status

Coverage decreased (-55.2%) to 37.16% when pulling e3cfb1a on msabramo:password_ask into 616a7d7 on sepulworld:master.

@coveralls
Copy link

coveralls commented Jul 15, 2016

Coverage Status

Coverage decreased (-55.2%) to 37.16% when pulling e3cfb1a on msabramo:password_ask into 616a7d7 on sepulworld:master.

def test_that_config_loads_from_yaml
assert_equal ({ server: '127.0.0.1', port: 8084 }),
@test_aptly_load.configure_with('test/fixtures/aptly-cli.yaml')
end

def test_that_config_loads_from_yaml_with_prompts
assert_equal ({:server=>"127.0.0.1", :port=>8084,
:username=>"zane", :password=>"${PROMPT}"}),

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.
Surrounding space missing for operator =>.
Space inside } missing.
Prefer single-quoted strings when you don't need string interpolation or special symbols.

@coveralls
Copy link

coveralls commented Jul 15, 2016

Coverage Status

Coverage decreased (-55.2%) to 37.16% when pulling dfe4908 on msabramo:password_ask into 616a7d7 on sepulworld:master.

@coveralls
Copy link

coveralls commented Jul 15, 2016

Coverage Status

Coverage decreased (-55.2%) to 37.16% when pulling dfe4908 on msabramo:password_ask into 616a7d7 on sepulworld:master.

location.to_s,
'test_1.0_amd64.deb',
'test/fixtures/test_1.0_amd64.deb')
AptlyCli::AptlyFile.new

Choose a reason for hiding this comment

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

Inconsistent indentation detected.

This allows not keeping passwords and such in the config file.

E.g.: In `/etc/aptly-cli.conf`:

```yaml
---
:proto: http
:server: 10.3.0.46
:port: 8082
:debug: false
:username: ${PROMPT}
:password: ${PROMPT_PASSWORD}
```

The tool will prompt for the specified values -- e.g.:

```
$ aptly-cli version
Enter a value for username:
zane
Enter a value for password:
***************
{"Version"=>"0.9.7"}
```

This also eliminates a bunch of duplicated code in all of the command
classes by having them inherit from a common base class. This was
actually essential because otherwise, every required file would trigger
a config load and thus prompting for any needed values over and over
again.
@coveralls
Copy link

coveralls commented Jul 15, 2016

Coverage Status

Coverage increased (+1.4%) to 93.784% when pulling d59de97 on msabramo:password_ask into 8161830 on sepulworld:master.

@coveralls
Copy link

coveralls commented Jul 15, 2016

Coverage Status

Coverage increased (+1.4%) to 93.784% when pulling d59de97 on msabramo:password_ask into 8161830 on sepulworld:master.

@@ -36,7 +43,8 @@ def configure_with(path_to_yaml_file)
config = YAML.load(IO.read(path_to_yaml_file))
rescue Errno::ENOENT
@log.warn(
'YAML configuration file couldn\'t be found at /etc/aptly-cli.conf. Using defaults.')
"YAML configuration file couldn\'t be found at " +

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.

This lets you specify a different config file. E.g.:

    aptly-cli -c ~/.config/aptly-cli/aptly-cli.conf repo_list
@coveralls
Copy link

coveralls commented Jul 15, 2016

Coverage Status

Coverage increased (+1.6%) to 94.046% when pulling 656ffe7 on msabramo:password_ask into 8161830 on sepulworld:master.

@coveralls
Copy link

coveralls commented Jul 15, 2016

Coverage Status

Coverage increased (+1.6%) to 94.046% when pulling 656ffe7 on msabramo:password_ask into 8161830 on sepulworld:master.

@msabramo msabramo changed the title Add prompting mechanism for config values Add prompting mechanism for config values and add --config global option Jul 15, 2016
@@ -0,0 +1,40 @@
class AptlyCommand
def initialize(config, options=nil)

Choose a reason for hiding this comment

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

Surrounding space missing in default value assignment.

@coveralls
Copy link

coveralls commented Jul 16, 2016

Coverage Status

Coverage decreased (-56.07%) to 36.337% when pulling bbccc36 on msabramo:password_ask into 8161830 on sepulworld:master.

This required a fairly big refactor, because I had to move stuff that
processes `${PROMPT}` and `${PROMPT_PASSWORD}` from `aptly_load.rb` to
`aptly_command.rb`, so that it could be used in the global options as
well as in the configuration file.
@coveralls
Copy link

coveralls commented Jul 16, 2016

Coverage Status

Coverage increased (+0.9%) to 93.287% when pulling 98a5497 on msabramo:password_ask into 8161830 on sepulworld:master.

@msabramo msabramo changed the title Add prompting mechanism for config values and add --config global option Add prompting mechanism for config values and add global options: --config, --server, --username, --password, --debug Jul 16, 2016
@coveralls
Copy link

coveralls commented Jul 16, 2016

Coverage Status

Coverage increased (+0.9%) to 93.287% when pulling 98a5497 on msabramo:password_ask into 8161830 on sepulworld:master.

@sepulworld
Copy link
Owner

This is great! Looks good first pass on my phone. I'll take a closer look this weekend or Monday for sure.

keyring = Keyring.new
value = keyring.get_password(@config[:server], @config[:username])

if !value

Choose a reason for hiding this comment

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

Favor unless over if for negative conditions.

@coveralls
Copy link

coveralls commented Jul 17, 2016

Coverage Status

Coverage decreased (-0.2%) to 92.177% when pulling 9ccd34c on msabramo:password_ask into 8161830 on sepulworld:master.

@coveralls
Copy link

coveralls commented Jul 17, 2016

Coverage Status

Coverage decreased (-0.2%) to 92.177% when pulling 9ccd34c on msabramo:password_ask into 8161830 on sepulworld:master.

Optionally use the [`keyring` gem](https://github.com/jheiss/keyring)
when `${KEYRING}` is specified as a value for an option or config
setting.

Note that I require `keyring` at the point of use, so that it is an
optional dependency.
@coveralls
Copy link

coveralls commented Jul 17, 2016

Coverage Status

Coverage decreased (-0.2%) to 92.177% when pulling a1bfbb0 on msabramo:password_ask into 8161830 on sepulworld:master.

@sepulworld
Copy link
Owner

Ok, this looks good to merge to me! Should we add your pull request notes to the README.md for docs too? I can merge this and #101 then release gem 0.3.0. Ill tag the release with notes on all of the changed add. Thanks again, this is a great security and functionality addition!

@sepulworld sepulworld merged commit a1bfbb0 into sepulworld:master Jul 19, 2016
@sepulworld
Copy link
Owner

@sepulworld
Copy link
Owner

#102 for adding to README.md this feature

@msabramo
Copy link
Contributor Author

#105 documents the keyring stuff in README.md

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.

None yet

4 participants