Bug #4510
closedCan't create auth_source_ldap with v1 and v2 api
Description
Foreman 1.4.1
Can't create auth_source_ldap with api
POSTING:
{
"auth_source_ldap": {
"type": "AuthSourceLdap",
"name": "ldap1",
"host": "ldap.example.com",
}
}
With the following url's
foreman.localdomain/api/auth_source_ldaps
foreman.localdomain/api/v1/auth_source_ldaps
foreman.localdomain/api/v2/auth_source_ldaps
Alway's results in the following error: (doesn't matter how many parameters I specify)
{
"auth_source_ldap": {
"id": null,
"errors": {
"name": [
"can't be blank"
],
"host": [
"can't be blank"
]
},
"full_messages": [
"Name can't be blank",
"Server can't be blank"
]
}
}
In production.log only the following error is shown.
Started POST "/api/auth_source_ldaps" for 192.168.88.1 at 2014-03-01 01:12:46 -0800
Processing by Api::V1::AuthSourceLdapsController#create as JSON
Parameters: {"apiv"=>"v1"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Setting current user thread-local variable to admin
(0.3ms) BEGIN
AuthSource Exists (0.5ms) SELECT 1 AS one FROM "auth_sources" WHERE "auth_sources"."name" = '' LIMIT 1
(0.1ms) ROLLBACK
Unprocessable entity AuthSourceLdap (id: new):
Name can't be blank
Server can't be blank
Rendered api/v1/errors/unprocessable_entity.json.rabl (1.5ms)
Body: {"auth_source_ldap":{"id":null,"errors":{"name":["can't be blank"],"host":["can't be blank"]},"full_messages":["Name can't be blank","Server can't be blank"]}}
Completed 422 Unprocessable Entity in 303ms (Views: 2.6ms | ActiveRecord: 1.8ms)
Also updating an auth_source_ldap doesn't work, no error occurs but changing port from 389 to 3890 resuls in the following loggin and no change.
Started GET "/api/v1/auth_source_ldaps/4" for 192.168.88.1 at 2014-03-01 02:06:06 -0800
Processing by Api::V1::AuthSourceLdapsController#show as JSON
Parameters: {"apiv"=>"v1", "id"=>"4"}
^[[1m^[[35mUser Load (0.5ms)^[[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Setting current user thread-local variable to admin
^[[1m^[[36mAuthSourceLdap Load (0.5ms)^[[0m ^[[1mSELECT "auth_sources".* FROM "auth_sources" WHERE "auth_sources"."type" IN ('AuthSourceLdap') AND "auth_sources"."id" = 4 LIMIT 1^[[0m
Rendered api/v1/auth_source_ldaps/show.json.rabl (90.0ms)
Body: {"auth_source_ldap":{"id":4,"type":"AuthSourceLdap","name":"ldap","host":"ldap.example.com","port":389,"account":"","base_dn":"","ldap_filter":"","attr_login":"","attr_firstname":"","attr_lastname":"","attr_mail":"","onthefly_register":false,"tls":false,"created_at":"2014-03-01T09:31:06Z","updated_at":"2014-03-01T09:31:06Z"}}
Completed 200 OK in 95ms (Views: 90.8ms | ActiveRecord: 1.0ms)
Started PUT "/api/v1/auth_source_ldaps/4" for 192.168.88.1 at 2014-03-01 02:06:20 -0800
Processing by Api::V1::AuthSourceLdapsController#update as JSON
Parameters: {"apiv"=>"v1", "id"=>"4"}
^[[1m^[[35mUser Load (0.5ms)^[[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Setting current user thread-local variable to admin
^[[1m^[[36mAuthSourceLdap Load (0.7ms)^[[0m ^[[1mSELECT "auth_sources".* FROM "auth_sources" WHERE "auth_sources"."type" IN ('AuthSourceLdap') AND "auth_sources"."id" = 4 LIMIT 1^[[0m
^[[1m^[[35m (0.3ms)^[[0m BEGIN
^[[1m^[[36mAuthSource Exists (0.5ms)^[[0m ^[[1mSELECT 1 AS one FROM "auth_sources" WHERE ("auth_sources"."name" = 'ldap' AND "auth_sources"."id" != 4) LIMIT 1^[[0m
^[[1m^[[35m (0.3ms)^[[0m COMMIT
Body: {"auth_source_ldap":{"account":"","account_password":"","attr_firstname":"","attr_lastname":"","attr_login":"","attr_mail":"","base_dn":"","created_at":"2014-03-01T09:31:06Z","host":"ldap.example.com","id":4,"ldap_filter":"","name":"ldap","onthefly_register":false,"port":389,"tls":false,"updated_at":"2014-03-01T09:31:06Z"}}
Completed 200 OK in 12ms (Views: 0.8ms | ActiveRecord: 0.0ms)
Files
Updated by Dominic Cleal almost 12 years ago
- Description updated (diff)
- Category set to API
- Translation missing: en.field_release deleted (
4)
Try getting rid of the outer hash when POSTing, I just tried submitting this and it worked:
POST /api/v2/auth_source_ldaps
{"name":"foo","host":"foo.example.com"}
Status: 201 Created
Updated by Dominic Cleal almost 12 years ago
- Status changed from New to Feedback
Oh, and be sure to specify "Content-Type: application/json" since you're uploading JSON.
Updated by Ronny M almost 12 years ago
This is what I tried (but now with your example) but keep getting the same error.
curl -s -H "Accept:application/json" -k -u admin:changeme -X POST {"name":"foo","host":"foo.example.com"} https://foreman.localdomain/api/v2/auth_source_ldaps
{
"error": {"id":null,"errors":{"name":["can't be blank"],"host":["can't be blank"]},"full_messages":["Name can't be blank","Server can't be blank"]}
}
[root@foreman foreman]# curl -s -H "Accept:application/json" -k -u admin:changeme -X POST {"name":"foo","host":"foo.example.com"} https://foreman.localdomain/api/v1/auth_source_ldaps
{"auth_source_ldap":{"id":null,"errors":{"name":["can't be blank"],"host":["can't be blank"]},"full_messages":["Name can't be blank","Server can't be blank"]}}
Started POST "/api/v2/auth_source_ldaps" for 192.168.88.135 at 2014-03-03 02:13:12 -0800
Processing by Api::V2::AuthSourceLdapsController#create as JSON
Parameters: {"apiv"=>"v2"}
^[[1m^[[36mUser Load (0.7ms)^[[0m ^[[1mSELECT "users".* FROM "users" WHERE "users"."login" = 'admin' LIMIT 1^[[0m
^[[1m^[[35mAuthSource Load (0.5ms)^[[0m SELECT "auth_sources".* FROM "auth_sources" WHERE "auth_sources"."id" = 1 LIMIT 1
^[[1m^[[36mCACHE (0.0ms)^[[0m ^[[1mSELECT "users".* FROM "users" WHERE "users"."login" = 'admin' LIMIT 1^[[0m
Authenticated user Admin User against INTERNAL authentication source
^[[1m^[[35mUser Load (0.5ms)^[[0m SELECT "users".* FROM "users" WHERE "users"."login" = 'admin' ORDER BY firstname LIMIT 1
Setting current user thread-local variable to admin
^[[1m^[[36m (0.2ms)^[[0m ^[[1mBEGIN^[[0m
^[[1m^[[35m (0.5ms)^[[0m UPDATE "users" SET "last_login_on" = '2014-03-03 10:13:12.084128', "updated_at" = '2014-03-03 10:13:12.085458' WHERE "users"."id" = 1
^[[1m^[[36m (1.1ms)^[[0m ^[[1mCOMMIT^[[0m
^[[1m^[[35mRole Load (0.5ms)^[[0m SELECT "roles".* FROM "roles" WHERE "roles"."name" = 'Anonymous' LIMIT 1
^[[1m^[[36m (0.3ms)^[[0m ^[[1mSELECT "roles".id FROM "roles" INNER JOIN "user_roles" ON "roles"."id" = "user_roles"."role_id" WHERE "user_roles"."user_id" = 1^[[0m
^[[1m^[[35mCACHE (0.0ms)^[[0m SELECT "roles".* FROM "roles" WHERE "roles"."name" = 'Anonymous' LIMIT 1
^[[1m^[[36mRole Exists (0.3ms)^[[0m ^[[1mSELECT 1 AS one FROM "roles" INNER JOIN "user_roles" ON "roles"."id" = "user_roles"."role_id" WHERE "user_roles"."user_id" = 1 AND "roles"."id" = 8 LIMIT 1^[[0m
Setting current user thread-local variable to admin
Setting current user thread-local variable to nil
^[[1m^[[35mUser Load (0.3ms)^[[0m SELECT "users".* FROM "users" WHERE "users"."login" = 'admin' LIMIT 1
Authorized user admin(Admin User)
Setting current user thread-local variable to admin
^[[1m^[[36m (0.1ms)^[[0m ^[[1mBEGIN^[[0m
^[[1m^[[35mAuthSource Exists (0.3ms)^[[0m SELECT 1 AS one FROM "auth_sources" WHERE "auth_sources"."name" = '' LIMIT 1
^[[1m^[[36m (0.1ms)^[[0m ^[[1mROLLBACK^[[0m
Unprocessable entity AuthSourceLdap (id: new):
Name can't be blank
Server can't be blank
Rendered api/v2/errors/unprocessable_entity.json.rabl within api/v2/layouts/error_layout (0.9ms)
Body: {
"error": {"id":null,"errors":{"name":["can't be blank"],"host":["can't be blank"]},"full_messages":["Name can't be blank","Server can't be blank"]}
}
Completed 422 Unprocessable Entity in 22ms (Views: 1.4ms | ActiveRecord: 5.5ms)
Started POST "/api/v1/auth_source_ldaps" for 192.168.88.135 at 2014-03-03 02:13:47 -0800
Processing by Api::V1::AuthSourceLdapsController#create as JSON
Parameters: {"apiv"=>"v1"}
^[[1m^[[35mUser Load (0.5ms)^[[0m SELECT "users".* FROM "users" WHERE "users"."login" = 'admin' LIMIT 1
^[[1m^[[36mAuthSource Load (0.3ms)^[[0m ^[[1mSELECT "auth_sources".* FROM "auth_sources" WHERE "auth_sources"."id" = 1 LIMIT 1^[[0m
^[[1m^[[35mCACHE (0.0ms)^[[0m SELECT "users".* FROM "users" WHERE "users"."login" = 'admin' LIMIT 1
Authenticated user Admin User against INTERNAL authentication source
^[[1m^[[36mUser Load (0.4ms)^[[0m ^[[1mSELECT "users".* FROM "users" WHERE "users"."login" = 'admin' ORDER BY firstname LIMIT 1^[[0m
Setting current user thread-local variable to admin
^[[1m^[[35m (0.2ms)^[[0m BEGIN
^[[1m^[[36m (0.3ms)^[[0m ^[[1mUPDATE "users" SET "last_login_on" = '2014-03-03 10:13:47.754480', "updated_at" = '2014-03-03 10:13:47.755214' WHERE "users"."id" = 1^[[0m
^[[1m^[[35m (0.7ms)^[[0m COMMIT
^[[1m^[[36mRole Load (0.3ms)^[[0m ^[[1mSELECT "roles".* FROM "roles" WHERE "roles"."name" = 'Anonymous' LIMIT 1^[[0m
^[[1m^[[35m (0.3ms)^[[0m SELECT "roles".id FROM "roles" INNER JOIN "user_roles" ON "roles"."id" = "user_roles"."role_id" WHERE "user_roles"."user_id" = 1
^[[1m^[[36mCACHE (0.0ms)^[[0m ^[[1mSELECT "roles".* FROM "roles" WHERE "roles"."name" = 'Anonymous' LIMIT 1^[[0m
^[[1m^[[35mRole Exists (0.3ms)^[[0m SELECT 1 AS one FROM "roles" INNER JOIN "user_roles" ON "roles"."id" = "user_roles"."role_id" WHERE "user_roles"."user_id" = 1 AND "roles"."id" = 8 LIMIT 1
Setting current user thread-local variable to admin
Setting current user thread-local variable to nil
^[[1m^[[36mUser Load (0.3ms)^[[0m ^[[1mSELECT "users".* FROM "users" WHERE "users"."login" = 'admin' LIMIT 1^[[0m
Authorized user admin(Admin User)
Setting current user thread-local variable to admin
^[[1m^[[35m (0.1ms)^[[0m BEGIN
^[[1m^[[36mAuthSource Exists (0.3ms)^[[0m ^[[1mSELECT 1 AS one FROM "auth_sources" WHERE "auth_sources"."name" = '' LIMIT 1^[[0m
^[[1m^[[35m (0.1ms)^[[0m ROLLBACK
Unprocessable entity AuthSourceLdap (id: new):
Name can't be blank
Server can't be blank
Rendered api/v1/errors/unprocessable_entity.json.rabl (1.0ms)
Body: {"auth_source_ldap":{"id":null,"errors":{"name":["can't be blank"],"host":["can't be blank"]},"full_messages":["Name can't be blank","Server can't be blank"]}}
Completed 422 Unprocessable Entity in 16ms (Views: 1.6ms | ActiveRecord: 4.1ms)
The logging from earlier post was created when I switched from command line to testing with google chrome postman.
I might have forgotten to pass the application/json param/header there but didn't notice because I got same error result as when using command line.
Updated by Ronny M almost 12 years ago
This error also occurs when using -d between POST and json. (I see I forgot them in the tests above)
Updated by Dominic Cleal almost 12 years ago
Your curl examples are missing the Content-Type header. Note that "Accept" isn't the same (that's saying what content you accept back from the server) as "Content-Type" (which is the content you're sending). without this header, it'll assume you're using regular form encoded data instead.
Add
-H "Content-Type:application/json"
I also used Postman to test this earlier, see the screenshot for how I've set it up (Raw, application/json on the right).
Updated by Ronny M almost 12 years ago
Ok, yes that works :)
Totally forgot that one :(
Please close this one :)