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

GitLab auth: Support custom root CA for https certificates signed in house #346

Closed
exstral opened this issue Feb 7, 2017 · 16 comments
Closed
Labels
auth provider bug Something isn't working upstream This issue belongs to a library or component outside

Comments

@exstral
Copy link

exstral commented Feb 7, 2017

Hi,

I'm trying to use the GitLab authentication to authenticate with our own internal gitlab. It requests the auth correctly, I authorize it on GitLab but when it returns to the callback on HackMD gives me a 500 and logs this error:

InternalOAuthError: Failed to obtain access token
    at Strategy.OAuth2Strategy._createOAuthError (/hackmd/node_modules/passport-oauth2/lib/strategy.js:379:17)
    at /hackmd/node_modules/passport-oauth2/lib/strategy.js:166:45
    at /hackmd/node_modules/oauth/lib/oauth2.js:191:18
    at ClientRequest.<anonymous> (/hackmd/node_modules/oauth/lib/oauth2.js:162:5)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:188:7)
    at TLSSocket.socketErrorListener (_http_client.js:310:9)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:188:7)
    at emitErrorNT (net.js:1278:8)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

I am sending the following environment variables to HackMD:

- HMD_URL_ADDPORT=true
- HMD_DB_URL=postgres://hackmd:hackmdpass@postgres:5432/hackmd
- HMD_IMAGE_UPLOAD_TYPE=filesystem
- HMD_ALLOW_ANONYMOUS=false
- HMD_ALLOW_FREEURL=true
- HMD_EMAIL=false
- HMD_GITLAB_BASEURL=https://gitlab.internal/
- HMD_GITLAB_CLIENTID=0da7d9dc0edd9cb9f7 (altered when posting here)
- HMD_GITLAB_CLIENTSECRET=c9a4aed19d16c1b (altered when posting here)

We are running the latest master of HackMD (d6822dd) and latest Gitlab version 8.16.4.

Any ideas on what could be wrong?

@jackycute
Copy link
Member

Hi @esbite
If you're using GitLab.com to auth.
It should work without HMD_GITLAB_BASEURL env var.

@jackycute
Copy link
Member

Huh, if you're using self-hosted gitlab.
It seems not our application's issue from the logs you've posted.
Did you make good application scope in gitlab?
Maybe you should check gitlab logs?

@exstral
Copy link
Author

exstral commented Feb 7, 2017

Hmm. Yes we're using self hosted Gitlab. But Gitlab doesn't give me an error, I get returned correctly to the callback on HackMD with the following URL:
GET http://localhost:3000/auth/gitlab/callback?code=0c592d84f143266d85252fc4385c65c8a9824bcdd1438fc8871366e459e7696d

And then I see the 500 error from HackMD. That code parameter should be the access token, right?

@jackycute
Copy link
Member

Yeah, that should work.
Did you check the api or auth scope in gitlab settings of access tokens?

2017-02-07 10 40 16

@exstral
Copy link
Author

exstral commented Feb 7, 2017

Yes I read your other issue about not supporting only the read_user scope so I checked both. If I only checked the read_user it didn't work with Gitlab giving me an error.

Any other ideas how I can debug this?

@jackycute
Copy link
Member

jackycute commented Feb 7, 2017

Well, I just tested on my local server to auth via gitlab.com and it worked.

Could you check the connection logs before the OAuth error?
It would be really helpful if you could dig the the request and response logs between them.

I guess it might be some problem when the hackmd request with to code.
hackmd -> request for auth -> gitlab authorized with code back -> hackmd -> request for token and user profile with the given code -> failed.

@exstral
Copy link
Author

exstral commented Feb 8, 2017

Ah. Finally figured out what the underlying error was. Of course this is also a certificate problem (Error: unable to verify the first certificate). Because our internal Gitlab is also using a certificate signed by our custom root cert. And when the browser is accessing it to grant the first authorization it works. But when nodejs itself makes the request to get the access token it fails because of certificate error.

How can I supply our custom root cert here, like I did for LDAP? :)

@jackycute
Copy link
Member

I think that means you need to use SSL connection.
There will be two ways to achieve that.

  1. Build a proxy before hackmd, like nginx or apache and add cert on it.
  2. Enable our built-in SSL server option. But it's only available via the config.json for now.
    you will need to need to turn on usessl and set sslcapath (or maybe more) see more here.

@exstral
Copy link
Author

exstral commented Feb 8, 2017

Are you saying that I need to put HackMD behind SSL? That seems unnecessary to me.

All I need is for the outgoing OAuth request for https://gitlab.internal/oauth/access_token to be able to verify the certificate we have running on gitlab. Node cannot verify this using the public Root Certificate Authorities that are automatically included with node so it cannot open a https connection. Our certificate can only be verified using our own Root Certificate Authority since we have signed it ourselves, which is common practice in bigger organisations.

I'm new to Node so I'm not sure how to supply a custom root CA for a request, but seems like you would need to use a library like this.
https://www.npmjs.com/package/ssl-root-cas

Because this is a problem in node core, see issue here:
nodejs/node#4175

This issue actually seems to be just about fixed in the absolute latest nodejs release:
nodejs/node#11062

@exstral exstral changed the title GitLab auth: "InternalOAuthError: Failed to obtain access token" GitLab auth: Support custom root CA for https certificates signed in house Feb 8, 2017
@jackycute
Copy link
Member

So maybe you could try on node v7.5.0?

@exstral
Copy link
Author

exstral commented Feb 8, 2017

Haha, yeah, I guess I could. But do you yet officially support node 7? I mean the Dockerfile you have is based on 6.9 :)

@jackycute
Copy link
Member

Yes we do support node 7, actually our service is running it now.
The dockerfile use node 6 for better stability in LTS.

@jackycute
Copy link
Member

Hey @esbite how's going?

@exstral
Copy link
Author

exstral commented Mar 1, 2017

Sorry no time to test, we decided we are happy with LDAP auth for now.

@a60814billy a60814billy added the enhancement Wants to improvide an existing feature label Mar 1, 2017
@a60814billy
Copy link
Member

a60814billy commented Mar 1, 2017

I think the issue caused by node.js not trust self-signed CA.
Maybe you can set NODE_TLS_REJECT_UNAUTHORIZE=0 to bypass authorized check.

$ export NODE_TLS_REJECT_UNAUTHORIZED=0
$ node app.js

@a60814billy a60814billy removed the enhancement Wants to improvide an existing feature label Mar 1, 2017
@SISheogorath SISheogorath added bug Something isn't working upstream This issue belongs to a library or component outside labels Oct 10, 2017
@SISheogorath
Copy link
Contributor

Closed for now. The problem was related to NodeJS and a newer version of NodeJS resolves it.

The solution means installing HackMD with a newer node version. If the problem persists with a newer NodeJS version feel free to reopen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth provider bug Something isn't working upstream This issue belongs to a library or component outside
Projects
None yet
Development

No branches or pull requests

4 participants