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

Added unlock_keychain action #580

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/Actions.md
Expand Up @@ -230,6 +230,27 @@ create_keychain(
)
```

### `unlock_keychain`

Unlock existing keychain and add it to the keychain search list.

```ruby
unlock_keychain(
path: "/path/to/KeychainName.keychain",
password: "mysecret"
)
```

If the keychain file is located in the standard location `~/Library/Keychains`, then it is sufficient to provide the keychain file name, or file name with suffix.

```ruby
unlock_keychain(
path: "KeychainName",
password: "mysecret"
)
```


### `delete_keychain`

Delete a keychain, can be used after creating one with `create_keychain`.
Expand Down
96 changes: 96 additions & 0 deletions lib/fastlane/actions/unlock_keychain.rb
@@ -0,0 +1,96 @@
module Fastlane
module Actions
class UnlockKeychainAction < Action
def self.run(params)
@keychain_path = params[:path]
Copy link
Member

Choose a reason for hiding this comment

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

Is it necessary to use @keychain_path? You could pass the keychain path from the run method to the other methods, right?
I'd prefer to not store anything in the action itself.

add_to_search_list = params[:add_to_search_list]

if !keychainfile_exists?
raise "Could not find the keychain file: #{@keychain_path}".red
end

# add to search list if not already added
if add_to_search_list
add_keychain_to_search_list
end

escaped_path = @keychain_path.shellescape
escaped_password = params[:password].shellescape

commands = []
# unlock given keychain and disable lock and timeout
commands << Fastlane::Actions.sh("security unlock-keychain -p #{escaped_password} #{escaped_path}", log: false)

Choose a reason for hiding this comment

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

Line is too long. [119/80]

commands << Fastlane::Actions.sh("security set-keychain-settings #{escaped_path}", log: false)

Choose a reason for hiding this comment

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

Line is too long. [102/80]

commands
end

def self.add_keychain_to_search_list
escaped_path = @keychain_path.shellescape

result = Fastlane::Actions.sh("security list-keychains", log: false)

# add the keychain to the keychains list
# the basic strategy is to open the keychain file it with Keychain Access

Choose a reason for hiding this comment

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

Line is too long. [81/80]

if !result.include?(@keychain_path)
Copy link
Member

Choose a reason for hiding this comment

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

You can switch to unless instead of if !

commands = []
commands << Fastlane::Actions.sh("open #{escaped_path}")
commands
end
end

def self.keychainfile_exists?
possible_locations = []
possible_locations << @keychain_path
possible_locations << "~/Library/Keychains/#{@keychain_path}"
possible_locations << "~/Library/Keychains/#{@keychain_path}.keychain"

possible_locations.each do |location|
expaded_location = File.expand_path(location)
if File.exist?(expaded_location)
@keychain_path = expaded_location
return true
end
end
end

#####################################################
# @!group Documentation
#####################################################

def self.description
"Unlock a keychain"
end

def self.details
"Unlocks the give keychain file and it adds it to the keychain search list."

Choose a reason for hiding this comment

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

Line is too long. [84/80]

Copy link
Member

Choose a reason for hiding this comment

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

Duplicate it, remove the it before adds

end

def self.available_options
[
FastlaneCore::ConfigItem.new(key: :path,
env_name: "FL_UNLOCK_KEYCHAIN_PATH",
description: "Path to the Keychain file",
optional: false),
FastlaneCore::ConfigItem.new(key: :password,
env_name: "FL_UNLOCK_KEYCHAIN_PASSWORD",
description: "Keychain password",
optional: false),
FastlaneCore::ConfigItem.new(key: :add_to_search_list,
env_name: "FL_UNLOCK_KEYCHAIN_ADD_TO_SEARCH_LIST",

Choose a reason for hiding this comment

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

Line is too long. [89/80]

description: "Add to keychain search list",

Choose a reason for hiding this comment

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

Line is too long. [82/80]

is_string: false,
default_value: true)

]
end

def self.authors
["xfreebird"]
end

def self.is_supported?(platform)

Choose a reason for hiding this comment

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

Rename is_supported? to supported?.

platform == :ios
end
end
end
end