Skip to content

Commit

Permalink
[capistrano] Add ability to use etc.getpwuid instead of `etc.getlog…
Browse files Browse the repository at this point in the history
…in` to get the user name (#146)

* Do not use getlogin() function to obtain username

`getlogin()` obtains the logged-in username by looking up the current tty in `utmp` and reporting the login name found in that utmp record. If Capistrano is ran from Jenkins in the host it returns the user logged in to `pts/0`, which is not the same user as runs the Capistrano. Also Ruby docs recommends avoid using `getlogin()`:
>getlogin → String
>Returns the short user name of the currently logged in user. Unfortunately, it is often rather easy to fool ::getlogin.
>
>Avoid ::getlogin for security-related purposes.
>
>If ::getlogin fails, try ::getpwuid.
>
>See the unix manpage for getpwuid(3) for more detail.
http://ruby-doc.org/stdlib-2.2.0/libdoc/etc/rdoc/Etc.html#method-c-getlogin

* keep backward compat

* rubocop

* add doc

Co-authored-by: Hippolyte HENRY <zippolyte@users.noreply.github.com>
  • Loading branch information
rkul and zippolyte authored Jan 30, 2020
1 parent 309664f commit 0f2fa56
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 35 deletions.
52 changes: 24 additions & 28 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-01-13 15:42:46 -0500 using RuboCop version 0.49.1.
# on 2020-01-30 14:50:03 +0100 using RuboCop version 0.49.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -42,14 +42,17 @@ Layout/EmptyLineAfterMagicComment:
Exclude:
- 'dogapi.gemspec'

# Offense count: 2
# Offense count: 16
# Cop supports --auto-correct.
Layout/EmptyLines:
Exclude:
- 'config/deploy.rb'
- 'config/deploy/production.rb'
- 'config/deploy/staging.rb'
- 'lib/capistrano/datadog/v2.rb'
- 'lib/dogapi/facade.rb'

# Offense count: 89
# Offense count: 92
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines
Expand Down Expand Up @@ -129,7 +132,6 @@ Layout/SpaceAroundEqualsInParameterDefault:
- 'lib/dogapi/common.rb'
- 'lib/dogapi/event.rb'
- 'lib/dogapi/facade.rb'
- 'lib/dogapi/v1/dash.rb'
- 'lib/dogapi/v1/embed.rb'
- 'lib/dogapi/v1/event.rb'
- 'lib/dogapi/v1/metric.rb'
Expand Down Expand Up @@ -201,9 +203,9 @@ Lint/UselessAssignment:

# Offense count: 12
Metrics/AbcSize:
Max: 51
Max: 53

# Offense count: 28
# Offense count: 29
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 123
Expand All @@ -215,23 +217,23 @@ Metrics/ClassLength:

# Offense count: 3
Metrics/CyclomaticComplexity:
Max: 8
Max: 9

# Offense count: 16
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 34

# Offense count: 5
# Offense count: 7
# Configuration parameters: CountKeywordArgs.
Metrics/ParameterLists:
Max: 7

# Offense count: 2
Metrics/PerceivedComplexity:
Max: 9
Max: 10

# Offense count: 13
# Offense count: 19
Style/AccessorMethodName:
Exclude:
- 'lib/dogapi/facade.rb'
Expand All @@ -240,8 +242,8 @@ Style/AccessorMethodName:
- 'lib/dogapi/v1/dashboard.rb'
- 'lib/dogapi/v1/embed.rb'
- 'lib/dogapi/v1/screenboard.rb'
- 'lib/dogapi/v1/user.rb'
- 'lib/dogapi/v1/synthetics.rb'
- 'lib/dogapi/v1/user.rb'

# Offense count: 1
# Cop supports --auto-correct.
Expand Down Expand Up @@ -279,7 +281,7 @@ Style/ClassCheck:
- 'lib/dogapi/facade.rb'
- 'lib/dogapi/v1/event.rb'

# Offense count: 2
# Offense count: 3
# Cop supports --auto-correct.
Style/ClassMethods:
Exclude:
Expand All @@ -297,14 +299,6 @@ Style/ColonMethodCall:
- 'lib/capistrano/datadog.rb'
- 'lib/capistrano/datadog/v2.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, AllowInnerBackticks.
# SupportedStyles: backticks, percent_x, mixed
Style/CommandLiteral:
Exclude:
- 'lib/dogapi/common.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, SingleLineConditionsOnly, IncludeTernaryExpressions.
Expand All @@ -313,7 +307,7 @@ Style/ConditionalAssignment:
Exclude:
- 'lib/capistrano/datadog.rb'

# Offense count: 21
# Offense count: 20
# Cop supports --auto-correct.
Style/DefWithParentheses:
Exclude:
Expand All @@ -340,7 +334,7 @@ Style/GuardClause:
- 'lib/capistrano/datadog.rb'
- 'lib/dogapi/facade.rb'

# Offense count: 86
# Offense count: 88
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
Expand All @@ -361,7 +355,7 @@ Style/HashSyntax:
- 'lib/dogapi/v1/snapshot.rb'
- 'lib/dogapi/v1/tag.rb'

# Offense count: 15
# Offense count: 16
# Cop supports --auto-correct.
# Configuration parameters: MaxLineLength.
Style/IfUnlessModifier:
Expand All @@ -381,7 +375,7 @@ Style/LineEndConcatenation:
Exclude:
- 'dogapi.gemspec'

# Offense count: 18
# Offense count: 21
# Cop supports --auto-correct.
Style/MethodCallWithoutArgsParentheses:
Exclude:
Expand All @@ -396,7 +390,7 @@ Style/MultilineIfThen:
Exclude:
- 'lib/capistrano/datadog.rb'

# Offense count: 33
# Offense count: 37
# Cop supports --auto-correct.
Style/MutableConstant:
Enabled: false
Expand All @@ -416,12 +410,11 @@ Style/Not:
Exclude:
- 'lib/dogapi/v1/metric.rb'

# Offense count: 4
# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: PreferredDelimiters.
Style/PercentLiteralDelimiters:
Exclude:
- 'lib/dogapi/common.rb'
- 'spec/integration/event_spec.rb'
- 'spec/integration/monitor_spec.rb'

Expand Down Expand Up @@ -477,14 +470,17 @@ Style/SpecialGlobalVars:
Exclude:
- 'dogapi.gemspec'

# Offense count: 25
# Offense count: 35
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Style/StringLiterals:
Exclude:
- 'Capfile'
- 'Gemfile'
- 'Rakefile'
- 'config/deploy.rb'
- 'config/deploy/staging.rb'
- 'dogapi.gemspec'
- 'examples/Capfile'
- 'examples/custom_event.rb'
Expand Down
5 changes: 4 additions & 1 deletion lib/capistrano/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ To set up your Capfile:
# for instance, only push the production event
# set :datadog_event_filter, proc { |event, hosts| event.msg_title.include?('ran production') ? [event, hosts] : nil }

# (optional) use the `Etc.getlogin` method (default) or the `Etc.getpwuid` method to get the user name
# default: use `Etc.getlogin`
# set :use_getlogin, true

You can find your Datadog API key [here](https://app.datadoghq.com/account/settings#api). If you don't have a Datadog account, you can sign up for one [here](http://www.datadoghq.com/).

`capistrano/datadog` will capture each Capistrano task that that Capfile runs, including the roles that the task applies to and any logging output that it emits and submits them as events to Datadog at the end of the execution of all the tasks. If sending to Datadog fails for any reason, your scripts will still succeed.

8 changes: 4 additions & 4 deletions lib/capistrano/datadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def self.cap_version()
@cap_version
end

def self.submit(api_key)
def self.submit(api_key, use_getlogin=true)
begin
if api_key
dog = Dogapi::Client.new(api_key)
reporter.report.each do |event, hosts|
reporter.report(use_getlogin).each do |event, hosts|
if hosts.size > 0
hosts.each do |host|
dog.emit_event event, host: host
Expand Down Expand Up @@ -93,9 +93,9 @@ def record_hostname(hostname)
end
end

def report()
def report(use_getlogin=true)
hostname = Dogapi.find_localhost
user = Etc.getlogin
user = use_getlogin ? Etc.getlogin : Etc.getpwuid.name

# Lazy randomness
aggregation_key = Digest::MD5.hexdigest "#{Time.new}|#{rand}"
Expand Down
6 changes: 5 additions & 1 deletion lib/capistrano/datadog/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ def puts(message)
namespace :datadog do
desc 'Submit the tasks that have run to Datadog as events'
task :submit do |ns|
Capistrano::Datadog.submit variables[:datadog_api_key]
if variables[:use_getlogin].nil?
Capistrano::Datadog.submit variables[:datadog_api_key]
else
Capistrano::Datadog.submit variables[:datadog_api_key], variables[:use_getlogin]
end
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion lib/capistrano/datadog/v3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,10 @@ def initialize(oio)

at_exit do
api_key = Capistrano::Configuration.env.fetch :datadog_api_key
Capistrano::Datadog.submit api_key
use_getlogin = Capistrano::Configuration.env.fetch :use_getlogin
if use_getlogin.nil?
Capistrano::Datadog.submit api_key
else
Capistrano::Datadog.submit api_key, use_getlogin
end
end

0 comments on commit 0f2fa56

Please sign in to comment.