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

Multiple Gitlab.client objects appear to interfere with each other #94

Closed
CCTJNII opened this issue Dec 11, 2014 · 11 comments
Closed

Multiple Gitlab.client objects appear to interfere with each other #94

CCTJNII opened this issue Dec 11, 2014 · 11 comments

Comments

@CCTJNII
Copy link

CCTJNII commented Dec 11, 2014

The config data appears to not be unique across Gitlab.client objects:

[30] pry(main)> g1 = Gitlab.client(:endpoint => 'https://gitlab1', :private_token => 'token1')
=> #<Gitlab::Client:0x007f277470b2f8 @endpoint="https://gitlab1", @private_token="token1", @sudo=nil, @user_agent="Gitlab Ruby Gem 3.2.0">
[31] pry(main)> g1.projects.length
=> 20
[32] pry(main)> g2 = Gitlab.client(:endpoint => 'https://gitlab2', :private_token => 'token2')
=> #<Gitlab::Client:0x007f27754a37f8 @endpoint="https://gitlab2", @private_token="token2", @sudo=nil, @user_agent="Gitlab Ruby Gem 3.2.0">
[33] pry(main)> g1.projects.length
Gitlab::Error::Unauthorized: Server responded with code 401, message: 401 Unauthorized. Request URI: https://gitlab1/api/v3/projects
from /usr/local/lib/ruby/gems/2.1.0/gems/gitlab-3.2.0/lib/gitlab/request.rb:61:in `validate'

I would expect that instantiating a new Gitlab.client object not change existing class behavior.

@NARKOZ
Copy link
Owner

NARKOZ commented Dec 13, 2014

It shouldn't.

Request URI: https://gitlab1/api/v3/projects

Error shows that endpoint didn't change, is that true? What is the value of g1.private_token?

@asedge
Copy link
Collaborator

asedge commented Dec 14, 2014

I did some testing and, for me, the endpoint displayed in the error is always the last one that was used during instantiation. I can't reproduce the error output shown above.

If a relative path is passed, HTTParty prepends the base_uri (we set it here: https://github.com/NARKOZ/gitlab/blob/master/lib/gitlab/request.rb#L84). If we pass the full URI when calling get, post etc. - we get the desired behavior.

@asedge
Copy link
Collaborator

asedge commented Dec 14, 2014

I pushed up the code I was playing with that fixes this issue (see linked commit above) - provided we can get confirmation the problem lies with the endpoint. Once confirmed and if there's no issue with how I approached the fix, I would be happy to merge it into master.

@CCTJNII
Copy link
Author

CCTJNII commented Dec 15, 2014

I retested and it does look like the endpoint, though the g1.endpoint variable doesn't change. I used variables this time to avoid any anonymization errors:

[43] pry(main)> g1 = Gitlab.client(:endpoint => host1, :private_token => token1)
=> #<Gitlab::Client:0x007f365e3ac2a8
 @endpoint="https://<gitlab1>/api/v3/",
 @private_token="<token1>",
 @sudo=nil,
 @user_agent="Gitlab Ruby Gem 3.2.0">
[44] pry(main)> g1.projects.length
=> 20
[45] pry(main)> g2 = Gitlab.client(:endpoint => "host", :private_token => "bar")
=> #<Gitlab::Client:0x007f365e454e30 @endpoint="host", @private_token="bar", @sudo=nil, @user_agent="Gitlab Ruby Gem 3.2.0">
[46] pry(main)> g1.projects.length
SocketError: getaddrinfo: Name or service not known
from /usr/local/lib/ruby/2.1.0/net/http.rb:879:in `initialize'
[47] pry(main)> g1.endpoint == host1
=> true
[48] pry(main)> g1.private_token == token1
=> true

@asedge
Copy link
Collaborator

asedge commented Dec 16, 2014

@CCTJNII Thanks for retesting. g1.endpoint shouldn't change - the problem lies elsewhere.

@NARKOZ - how do you feel about the linked/proposed fix?

NARKOZ added a commit that referenced this issue Dec 17, 2014
@NARKOZ
Copy link
Owner

NARKOZ commented Dec 17, 2014

Added a spec in 1ec8b38 and it's passing.

I don't think it requires any fixes.

@NARKOZ NARKOZ closed this as completed Dec 17, 2014
@asedge
Copy link
Collaborator

asedge commented Dec 17, 2014

@NARKOZ I still think it's an issue. Here's (hopefully) a better illustration of the problem:

irb(main):003:0> g1 = Gitlab.client(endpoint: 'http://foo', private_token: 'foo')
=> #<Gitlab::Client:0x00000002cddb68 @endpoint="http://foo", @private_token="foo", @user_agent="Gitlab Ruby Gem 3.2.0", @sudo=nil, @httparty=nil>
irb(main):004:0> Gitlab::Client.base_uri
=> "http://foo"
irb(main):005:0> g2 = Gitlab.client(endpoint: 'http://bar', private_token: 'bar')
=> #<Gitlab::Client:0x00000002bddb28 @endpoint="http://bar", @private_token="bar", @user_agent="Gitlab Ruby Gem 3.2.0", @sudo=nil, @httparty=nil>
irb(main):006:0> Gitlab::Client.base_uri
=> "http://bar"

If you don't pass an absolute URL to HTTParty::get (for example), the base_uri is automatically prefixed to the relative path. The endpoint is passed to HTTParty::base_uri during instantiation by Gitlab::Request#set_request_defaults.

There may be a better way to handle this than the fix I proposed.

@dblessing
Copy link
Contributor

Yes, base_uri is the issue, not endpoint directly. The real question in my mind is whether anyone will ever be communicating with 2 distinct GitLab instances. It may be a minor issue.

At any rate, I found a report of this in HTTParty and the maintainer is not interested in making base_uri an instance variable. They suggest prefixing the endpoint in each 'get', etc method.

@NARKOZ NARKOZ reopened this Dec 18, 2014
@NARKOZ
Copy link
Owner

NARKOZ commented Dec 18, 2014

@asedge can you push a fix?

@asedge asedge closed this as completed in 51611fe Dec 18, 2014
@asedge
Copy link
Collaborator

asedge commented Dec 18, 2014

Done.

@NARKOZ
Copy link
Owner

NARKOZ commented Dec 18, 2014

Thanks.

Nihad Abbasov

On Thu, Dec 18, 2014 at 6:38 PM, Sean Edge notifications@github.com wrote:

Done.


Reply to this email directly or view it on GitHub
#94 (comment).

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

No branches or pull requests

4 participants