Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ api = Bigcommerce::Api.new({
})
```

You can also enable SSL for the Legacy API.
By default SSL is always enabled for the Legacy API. Remember that the fields `:ssl_client_cert`, `:ssl_client`, `:ssl_ca_file` and `:verify_peer` are all required when specifying your own self-signed SSL certificate.

```rb
require 'bigcommerce'
Expand All @@ -87,14 +87,28 @@ api = Bigcommerce::Api.new({
:store_url => "https://store.mybigcommerce.com",
:username => "username",
:api_key => "api_key",
:ssl_client_cert => OpenSSL::X509::Certificate.new(File.read("cert.pem")),
:ssl_client_key => OpenSSL::PKey::RSA.new(File.read("key.pem"), "passphrase, if any"),
:ssl_ca_file => "ca_certificate.pem",
:verify_ssl => OpenSSL::SSL::VERIFY_PEER
:ssl_client_cert => "/path/to/cert.pem",
:ssl_client => { path: "/path/to/key.pem", passphrase: "passphrase, if any" },
:ssl_ca_file => "/path/to/ca_certificate.pem",
:verify_peer => OpenSSL::SSL::VERIFY_PEER
})
```

Disabling the SSL verification is only advisable in development environment and it can be done by explicitly passing `OpenSSL::SSL::VERIFY_NONE` value in `:verify_peer` field as below:

```rb
require 'bigcommerce'

api = Bigcommerce::Api.new({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would this example benefit from having the other SSL options passed in as well?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@pedelman That's certainly a good suggestion from production environment point of view where API user is explicitly specifying SSL options. But to connect to BigCommerce through Legacy API we have 2 ways one without supplying SSL options and other with SSL options (even though in both cases SSL is verified). To disable verification we just need verify_peer and other options are not mandatory.
Let me know what you would prefer to go with and we could work on the same.

:store_url => "https://store.mybigcommerce.com",
:username => "username",
:api_key => "api_key",
:ssl_client_cert => "/path/to/cert.pem",
:ssl_client => { path: "/path/to/key.pem", passphrase: "passphrase, if any" },
:ssl_ca_file => "/path/to/ca_certificate.pem",
:verify_peer => OpenSSL::SSL::VERIFY_NONE
})
```
Remember that the fields `:ssl_client_cert`, `:ssl_client_key`, `:ssl_ca_file`
and `:verify_ssl` are all required when enabling SSL certificates.

## Connecting to the store

Expand Down
19 changes: 14 additions & 5 deletions lib/bigcommerce/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,27 @@ def verify_peer=(verify)
end

def ssl_ca_file=(path)
@configuration.ssl_ca_file = path
@configuration[:ssl_ca_file] = path
end

def ssl_client=(options)
if options[:passphrase].nil?
send(:ssl_client_key=, options[:path])
else
send(:ssl_client_key=, options[:path], options[:passphrase])
end
end

def ssl_client_key=(path,passphrase=nil)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For this method... I'd prefer to keep the initialize a little cleaner, I think we have two options.

  1. We can check to see if the path param is a hash, if it is, we can pull out the path / params.
  2. We can create a new method with a new key which will call the old version after pulling out the params.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@pedelman I like the second option better as going with first option would not be semantically correct, we would be expecting two parameters path and passphrase(optional) and always receive one(the hash) in path and then break it down from there into actual params which was our original expectation.
We could go with the second option and have a new key which calls the old version. Currently the option is called ssl_client_key and it has associated setter available. We could call the new key asssl_client_key_details or may be ssl_client.

Let me know what key name you prefer or if you have any suggestions. :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think I like ssl_client the best.

if passphrase.nil?
@configuration.ssl_client_key = OpenSSL::PKey::RSA.new(File.read(path))
@configuration[:ssl_client_key] = OpenSSL::PKey::RSA.new(File.read(path))
else
@configuration.ssl_client_key = OpenSSL::PKey::RSA.new(File.read(path), passphrase)
@configuration[:ssl_client_key] = OpenSSL::PKey::RSA.new(File.read(path), passphrase)
end
end

def ssl_client_cert=(path)
@configuration.client_cert = OpenSSL::X509::Certificate.new(File.read(path))
@configuration[:ssl_client_cert] = OpenSSL::X509::Certificate.new(File.read(path))
end

def get(path, options = {}, headers = {})
Expand Down Expand Up @@ -125,6 +133,7 @@ def request_legacy(method, path, options, headers={})
resource_options = {
:user => @configuration[:username],
:password => @configuration[:api_key],
:verify_ssl => @configuration[:verify_ssl] || OpenSSL::SSL::VERIFY_PEER,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this needed? The default for rest client is to enable peer, and if you want to disable it, you can just use the :verify_peer?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, I tested the code with only :verify_ssl => @configuration[:verify_ssl]. In the case, where :verify_peer is not passed in API config, @configuration[:verify_ssl] would return nil and RestClient treats it as OpenSSL::SSL::VERIFY_NONE. That's the reason for adding || OpenSSL::SSL::VERIFY_PEER part.

:headers => headers
}

Expand All @@ -133,7 +142,7 @@ def request_legacy(method, path, options, headers={})
if @configuration[:ssl_client_key] && @configuration[:ssl_client_cert] && @configuration[:ssl_ca_file]
rest_client = RestClient::Resource.new(
"#{@configuration[:store_url]}/api/v2#{path}.json",
:username => @configuration[:username],
:user => @configuration[:username],
:password => @configuration[:api_key],
:ssl_client_cert => @configuration[:ssl_client_cert],
:ssl_client_key => @configuration[:ssl_client_key],
Expand Down