# Enable github access via API

**URL:** https://forums.suse.com/t/enable-github-access-via-api/1866
**Category:** Rancher 1.x
**Created:** [February 26, 2016, 12:42pm UTC](https://forums.suse.com/t/enable-github-access-via-api/1866 "2016-02-26T12:42:01Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![clescot](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/clescot/32/317_2.png) [@clescot](https://forums.suse.com/u/clescot)
#### Post date: [February 26, 2016, 12:42pm UTC](https://forums.suse.com/t/enable-github-access-via-api/1866/1 "2016-02-26T12:42:01Z")

</div>

Hi,  
i try to enable github access control via an API call.  
To understand which calls needs to be done, i inspect the browser.  
I’ve seen that one first call is a POST to /v1/githubconfig  
with this body :  
`{"accessMode":"unrestricted","id":null,"type":"githubconfig","allowedIdentities":[],"clientId":"myclientid","clientSecret":"myclientsecret","enabled":false,"hostname":null,"name":"githubconfig","scheme":"https://"}`  
But when i execute this request, it returns a 403 response with this message:  
`InvalidCSRFToken : CSRF header and cookie do not match`

Is it possible to configure the rancher server from the start only via API calls?

best regards,

Charles.

---

<div class="post-metadata">

### Author: ![vincent](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/vincent/32/7156_2.png) [@vincent](https://forums.suse.com/u/vincent)
#### Post date: [February 26, 2016, 4:54pm UTC](https://forums.suse.com/t/enable-github-access-via-api/1866/2 "2016-02-26T16:54:24Z")

</div>

The UI is 100% static client-side code that just talks to the API. Anything it can do you can do, though some things are admittedly non-obvious or lacking documentation.

So that is the API call, but you’ll want `enabled:true`. The UI sends `false`, tries to authenticate, and then sets it to `true` if everything worked so that it is difficult to lock yourself out with invalid config.

The error message has nothing to do with setting up auth specifically… it means you’re sending a request that looks like it’s from a browser, where we need to protect against [cross-site request forgery](https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet) attacks.

To do that the API requires any non-GET request have the value of the `CSRF` cookie (which the server provides) sent back as the value of a `X-API-CSRF` header (which you can’t do if you’re code running from a different domain).

Since you’re (probably?) not a browser you can just remove where you’re sending a `User-Agent` header, or make it not contain “Mozilla”. If you _are_ a browser, on a page in a _different domain_ than Rancher, then you won’t be able to do this… because you’re trying to execute a CSRF attack against yourself 😄.

---

<div class="post-metadata">

### Author: ![clescot](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/clescot/32/317_2.png) [@clescot](https://forums.suse.com/u/clescot)
#### Post date: [February 26, 2016, 11:33pm UTC](https://forums.suse.com/t/enable-github-access-via-api/1866/3 "2016-02-26T23:33:02Z")

</div>

Hi @vincent,  
i’ve updated the query with enabled: true.

A 400 bad request is returned, but without any details…  
here is the curl command

```auto
curl 'http://myns:myport/v1/githubconfig' -H 'Host: myns:myport' -H 'Accept: application/json' --compressed -H 'Content-Type: application/json' -d "{"accessMode":"unrestricted","id":null,"type":"githubconfig","allowedIdentities":[],"clientId":"myclientId","clientSecret":"myClientSecret","enabled":true,"hostname":null,"name":"githubconfig","scheme":"https://"}"

```

In logs, a NullPointerException is thrown:

```auto
2016-02-26 23:28:21,195 ERROR [:] [] [] [] [p2029686239-127] [i.g.i.g.s.ApiRequestFilterDelegate] Unhandled exception in API for request [io.github.ibuildthecloud.gdapi.request.ApiRequest@8c2f627] java.lang.NullPointerException: null
    at io.cattle.platform.iaas.api.auditing.AuditServiceImpl.logRequest(AuditServiceImpl.java:86) ~[cattle-framework-auditing-0.5.0-SNAPSHOT.jar:na]
    at io.cattle.platform.iaas.api.auditing.AuditLogsRequestHandler.handle(AuditLogsRequestHandler.java:23) ~[cattle-framework-auditing-0.5.0-SNAPSHOT.jar:na]
    at io.github.ibuildthecloud.gdapi.servlet.ApiRequestFilterDelegate.doFilter(ApiRequestFilterDelegate.java:86) ~[cattle-framework-java-server-0.5.0-SNAPSHOT.jar:na]
    at io.cattle.platform.api.servlet.ApiRequestFilter$1.runInContext(ApiRequestFilter.java:90) [cattle-framework-api-0.5.0-SNAPSHOT.jar:na]
    at org.apache.cloudstack.managed.context.ManagedContextRunnable$1.run(ManagedContextRunnable.java:49) [cattle-framework-managed-context-0.5.0-SNAPSHOT.jar:na]
    at org.apache.cloudstack.managed.context.impl.DefaultManagedContext$1.call(DefaultManagedContext.java:55) [cattle-framework-managed-context-0.5.0-SNAPSHOT.jar:na]
    at org.apache.cloudstack.managed.context.impl.DefaultManagedContext.callWithContext(DefaultManagedContext.java:108) [cattle-framework-managed-context-0.5.0-SNAPSHOT.jar:na]
    at org.apache.cloudstack.managed.context.impl.DefaultManagedContext.runWithContext(DefaultManagedContext.java:52) [cattle-framework-managed-context-0.5.0-SNAPSHOT.jar:na]
    at org.apache.cloudstack.managed.context.ManagedContextRunnable.run(ManagedContextRunnable.java:46) [cattle-framework-managed-context-0.5.0-SNAPSHOT.jar:na]
    at io.cattle.platform.api.servlet.ApiRequestFilter.doFilter(ApiRequestFilter.java:83) [cattle-framework-api-0.5.0-SNAPSHOT.jar:na]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.servlets.UserAgentFilter.doFilter(UserAgentFilter.java:83) [jetty-servlets-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.servlets.GzipFilter.doFilter(GzipFilter.java:300) [jetty-servlets-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) [jetty-servlet-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [jetty-server-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577) [jetty-security-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223) [jetty-server-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) [jetty-server-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) [jetty-servlet-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) [jetty-server-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) [jetty-server-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [jetty-server-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.server.Server.handle(Server.java:499) [jetty-server-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310) [jetty-server-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) [jetty-server-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) [jetty-io-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.11.v20150529.jar:9.2.11.v20150529]
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) [jetty-util-9.2.11.v20150529.jar:9.2.11.v20150529]
    at java.lang.Thread.run(Thread.java:745) [na:1.7.0_95]

```

Have you any request example?

best regards,

Charles.

---

<div class="post-metadata">

### Author: ![wizardofmath](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/wizardofmath/32/23_2.png) [@wizardofmath](https://forums.suse.com/u/wizardofmath)
#### Post date: [March 1, 2016, 5:17pm UTC](https://forums.suse.com/t/enable-github-access-via-api/1866/4 "2016-03-01T17:17:36Z")

</div>

Using this request

```auto
curl 'http://localhost:8080/v1/githubconfig' -H 'Host: localhost:8080' -H 'Accept: application/json' --compressed -H 'Content-Type: application/json' -d "{"accessMode":"unrestricted","id":null,"type":"githubconfig","allowedIdentities":[],"clientId":"myclientID","clientSecret":"myclientSecrey","enabled":true,"hostname":null,"name":"githubconfig","scheme":"https://"}"

RESP:
{"id":"454b778f-919f-4c67-b677-71500751c52c","type":"error","links":{},"actions":{},"status":400,"code":"InvalidBodyContent","message":"InvalidBodyContent","detail":null}

```

However if you replace the surrounding `"` on the data for the request with `'` like so:

```auto
curl 'http://localhost:8080/v1/githubconfig' -H 'Host: localhost:8080' -H 'Accept: application/json' --compressed -H 'Content-Type: application/json' -d '{"accessMode":"unrestricted","id":null,"type":"githubconfig","allowedIdentities":[],"clientId":"myClientID","clientSecret":"myClientSecret","enabled":true,"hostname":null,"name":"githubconfig","scheme":"https://"}'

RESP:
{"id":null,"type":"githubconfig","links":{},"actions":{},"accessMode":"unrestricted","allowedIdentities":[],"clientId":"6a13fa4975b853d12458","clientSecret":null,"enabled":true,"hostname":null,"name":"githubconfig","scheme":"https://"}

```

A 400 is not returned.

---

<div class="post-metadata">

### Author: ![clescot](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/clescot/32/317_2.png) [@clescot](https://forums.suse.com/u/clescot)
#### Post date: [March 1, 2016, 10:06pm UTC](https://forums.suse.com/t/enable-github-access-via-api/1866/5 "2016-03-01T22:06:58Z")

</div>

Hi @wizardofmath,  
thank you very much for your support!  
It works!  
It was a newbie mistake…

best regards,

Charles.

---

<div class="post-metadata">

### Author: ![vincent](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/vincent/32/7156_2.png) [@vincent](https://forums.suse.com/u/vincent)
#### Post date: [March 1, 2016, 10:24pm UTC](https://forums.suse.com/t/enable-github-access-via-api/1866/6 "2016-03-01T22:24:52Z")

</div>

(Throwing a null pointer exception in this case is still a problem though, and will be fixed)
