Hi Sync Gateway users,
I am accessing the Sync Gateway through the Public REST API, authenticating with BasicAuth, and some of the requests are returned with 401 errors.
To be more precise, my datalogger sends a POST request (new document) to /{db}/ once per hour. The pattern is always the same: one request is accepted, then the next hour is refused, then next hour accepted again, etc, and it alternates strictly… I noticed this alternation if I increase the interval to 3 hours, also. But if I decrease the interval to a few minutes, all authentications go through and requests do proceed…
I got around it by sending a dummy GET /{db}
/ request just before doing the actual POST request, so the dummy request is at the front line and gets the refusals, and the POST request goes through. It usually works, but on some occasions both requests got refused with a 401 (!).
Is there anything in the way Sync Gateway handles BasicAuth that could explain this behaviour?
I am using Sync Gateway community edition 1.2.1-4 and had the same issues with 1.2.0-79
Here is an extract captured with tcpdump on port 4984:
GET /default/ HTTP/1.1
Host: XX.XX.XX.XX:4984
Authorization: Basic YWRtaW46YWRtaW4=
HTTP/1.1 401 Unauthorized
Content-Type: application/json
Server: Couchbase Sync Gateway/1.2.1
Www-Authenticate: Basic realm="Couchbase Sync Gateway"
Date: Thu, 19 May 2016 11:38:05 GMT
Content-Length: 49
{“error”:“Unauthorized”,“reason”:“Invalid login”}
POST /default/ HTTP/1.1
Host: XX.XX.XX.XX:4984
Authorization: Basic YWRtaW46YWRtaW4=
Content-Length: 105
{“type”:“sensor”,“id”:“123”,“time”:1463657910,“data”:[{“t”:1463654251,“d”:449},{“t”:1463656056,“d”:449}]}
HTTP/1.1 200 OK
Content-Length: 94
Content-Type: application/json
Etag: "1-7e8aabb8bbf760fc2a915d098641b496"
Location: 77de55209876ad610dde0afe94944605
Server: Couchbase Sync Gateway/1.2.1
Date: Thu, 19 May 2016 11:38:35 GMT
{“id”:“77de55209876ad610dde0afe94944605”,“ok”:true,“rev”:“1-7e8aabb8bbf760fc2a915d098641b496”}
and the corresponding Sync Gateway log:
11:38:05.811756 2016-05-19T11:38:05.811Z HTTP auth failed for username="admin"
11:38:05.811789 2016-05-19T11:38:05.811Z HTTP: #214: GET /default/
11:38:05.811823 2016-05-19T11:38:05.811Z HTTP: #214: --> 401 Invalid login (0.2 ms)
11:38:35.232664 2016-05-19T11:38:35.232Z HTTP: #215: POST /default/ (as admin)
11:38:35.234878 2016-05-19T11:38:35.234Z HTTP+: #215: --> 200 (3.8 ms)
other example:
POST /default/ HTTP/1.1
Host: 52.16.49.121:4984
Authorization: Basic YWRtaW46YWRtaW4=
Content-Length: 105
{“type”:“sensor”,“id”:“123”,“time”:1463560059,“data”:[{“t”:1463556432,“d”:450},{“t”:1463558237,“d”:449}]}
HTTP/1.1 401 Unauthorized
Content-Type: application/json
Server: Couchbase Sync Gateway/1.2.0
Www-Authenticate: Basic realm="Couchbase Sync Gateway"
Date: Wed, 18 May 2016 08:27:46 GMT
Content-Length: 49
{“error”:“Unauthorized”,“reason”:“Invalid login”}
POST /default/ HTTP/1.1
Host: 52.16.49.121:4984
Authorization: Basic YWRtaW46YWRtaW4=
Content-Length: 105
{“type”:“sensor”,“id”:“123”,“time”:1463563721,“data”:[{“t”:1463560093,“d”:449},{“t”:1463561898,“d”:450}]}
HTTP/1.1 200 OK
Content-Length: 94
Content-Type: application/json
Etag: "1-e02ebb784f9a46b99d6b024c1d29c7ec"
Location: 1403a87a6d0630e65c2484cd25f83394
Server: Couchbase Sync Gateway/1.2.0
Date: Wed, 18 May 2016 09:28:47 GMT
{“id”:“1403a87a6d0630e65c2484cd25f83394”,“ok”:true,“rev”:“1-e02ebb784f9a46b99d6b024c1d29c7ec”}
Any ideas?
Thanks!
Nico