Last active
January 9, 2017 09:59
-
-
Save tomtheun/8d037332efbe8a5c3e77f734d9b9df1c to your computer and use it in GitHub Desktop.
Simple Ruby script for resetting JIRA user passwords
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'securerandom' | |
require 'json' | |
require 'net/http' | |
options = {} | |
OptionParser.new do |parser| | |
parser.banner = 'Usage: jira-pw-reset.rb [options]' | |
parser.on('-h', '--help', 'Show this help message') do || | |
puts parser | |
end | |
parser.on('--url URL', 'JIRA base URL') do |v| options[:jira_url] = v end | |
parser.on('--username USERNAME', 'JIRA username (with administrator permissions)') do |v| options[:jira_username] = v end | |
parser.on('--password PASSWORD', 'JIRA password') do |v| options[:jira_password] = v end | |
parser.on('--users USER1,USER2,...', 'Comma seperated list of usernames to change the password for') do |v| options[:usernames] = v end | |
parser.on() | |
end.parse! | |
def ask_if_missing(option, fancy_name) | |
if !option | |
puts "#{fancy_name}:" | |
option = gets.chomp | |
if option.empty? | |
abort("#{fancy_name} is required") | |
end | |
end | |
option | |
end | |
jira_url = ask_if_missing options[:jira_url], 'JIRA base URL' | |
jira_username = ask_if_missing options[:jira_username], 'JIRA username' | |
jira_password = ask_if_missing options[:jira_password], 'JIRA password' | |
usernames = ask_if_missing options[:usernames], 'Usernames' | |
usernames = usernames.split(',').uniq.reject(&:empty?) | |
usernames.each { |username| | |
random_password = SecureRandom.hex | |
request_body = {:password => random_password } | |
puts "Resetting password for #{username} to: #{random_password}" | |
uri = URI.parse("#{jira_url}/rest/api/2/user/password?username=#{username}") | |
req = Net::HTTP::Put.new(uri) | |
req['Content-Type'] = 'application/json' | |
req.basic_auth jira_username, jira_password | |
req.body = JSON.generate(request_body) | |
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http| | |
http.request(req) | |
} | |
if res.code != "204" | |
puts "- Could not change password for user #{username}" | |
puts "- Got the following response code from server: #{res.code} (#{res.message})" | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment