2017-01-16 27 views
8

Tôi đang cố gắng sử dụng Salt-Api, vì vậy tôi đã tạo ra một salt-api.conf trong /etc/salt/master.d/ như sau:Cấu hình Salt API - Java

external_auth: 
    pam: 
    saltuser: 
     - .* 
     - '@wheel' # to allow access to all wheel modules 
     - '@runner' # to allow access to all runner modules 
     - '@jobs' # to allow access to the jobs runner and/or wheel module 

rest_cherrypy: 
    port: 8000 
    ssl_crt: /etc/pki/tls/certs/localhost.crt 
    ssl_key: /etc/pki/tls/certs/localhost.key 
    disable_ssl: True 
    webhook_disable_auth: True 
    webhook_url: /hook 

người sử dụng trong /etc/salt/master được thiết lập như user: root. Vì vậy, khi tôi cố gắng xác thực bằng pam địa phương hoạt động:

sudo salt -a pam '*' test.ping 
username: saltuser 
password: saltuser 

minion: 
    True 

Tuy nhiên khi tôi cố gắng sử dụng curl, nó không thành công:

curl -i http://localhost:8000/login -H "Accept: application/json" -d username='saltuser' -d password='saltuser' -d eauth='pam' 
HTTP/1.1 401 Unauthorized 
Content-Length: 760 
Access-Control-Expose-Headers: GET, POST 
Vary: Accept-Encoding 
Server: CherryPy/3.5.0 
Allow: GET, HEAD, POST 
Access-Control-Allow-Credentials: true 
Date: Mon, 16 Jan 2017 05:51:48 GMT 
Access-Control-Allow-Origin: * 
Content-Type: text/html;charset=utf-8 
Set-Cookie: session_id=f4c747f23e95ea7742a11a6e6cef146b91a31737; expires=Mon, 16 Jan 2017 15:51:48 GMT; Path=/ 

<!DOCTYPE html PUBLIC 
"-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta> 
    <title>401 Unauthorized</title> 
    <style type="text/css"> 
    #powered_by { 
     margin-top: 20px; 
     border-top: 2px solid black; 
     font-style: italic; 
    } 

    #traceback { 
     color: red; 
    } 
    </style> 
</head> 
    <body> 
     <h2>401 Unauthorized</h2> 
     <p>Could not authenticate using provided credentials</p> 
     <pre id="traceback"></pre> 
    <div id="powered_by"> 
     <span> 
     Powered by <a href="http://www.cherrypy.org">CherryPy 3.5.0</a> 
     </span> 
    </div> 
    </body> 
</html> 

Vì vậy, tôi không thể có một trong hai khách hàng Java hoặc Python khách hàng được kết nối. Tôi đang thiếu gì trong cấu hình của mình? salt-master đã chạy dưới dạng gốc. Từ mã Java của tôi:

import com.suse.salt.netapi.AuthModule; 
import com.suse.salt.netapi.calls.WheelResult; 
import com.suse.salt.netapi.calls.wheel.Key; 
import com.suse.salt.netapi.client.SaltClient; 
import com.suse.salt.netapi.exception.SaltException; 

import java.net.URI; 
import java.util.Optional; 

/** 
* Example code calling wheel functions. 
*/ 
public class Salt { 

    private static final String SALT_API_URL = " http://localhost:8000"; 
    private static final String USER = "saltuser"; 
    private static final String PASSWORD = "saltuser"; 

    public static void main(String[] args) throws SaltException { 
     // Init the client 
     SaltClient client = new SaltClient(URI.create(SALT_API_URL)); 

     // List accepted and pending minion keys 
     WheelResult<Key.Names> keyResults = Key.listAll().callSync(
       client, USER, PASSWORD, AuthModule.AUTO); 
     Key.Names keys = keyResults.getData().getResult(); 

     System.out.println("\n--> Accepted minion keys:\n"); 
     keys.getMinions().forEach(System.out::println); 
     System.out.println("\n--> Pending minion keys:\n"); 
     keys.getUnacceptedMinions().forEach(System.out::println); 

     // Generate a new key pair and accept the public key 
     WheelResult<Key.Pair> genResults = Key.genAccept("new.minion.id", Optional.empty()) 
       .callSync(client, USER, PASSWORD, AuthModule.AUTO); 
     Key.Pair keyPair = genResults.getData().getResult(); 

     System.out.println("\n--> New key pair:"); 
     System.out.println("\nPUB:\n\n" + keyPair.getPub()); 
     System.out.println("\nPRIV:\n\n" + keyPair.getPriv()); 
    } 
} 

com.suse.salt.netapi.exception.SaltUserUnauthorizedException: Salt user does not have sufficient permissions 
    at com.suse.salt.netapi.client.impl.HttpClientConnection.createSaltException(HttpClientConnection.java:217) 
    at com.suse.salt.netapi.client.impl.HttpClientConnection.executeRequest(HttpClientConnection.java:204) 
    at com.suse.salt.netapi.client.impl.HttpClientConnection.request(HttpClientConnection.java:85) 
    at com.suse.salt.netapi.client.impl.HttpClientConnection.getResult(HttpClientConnection.java:73) 
+1

Có bất kỳ nỗ lực xác thực nào trong /var/log/auth.log không? – kwarunek

Trả lời

3

Bạn nhận được 401 Unauthorized bởi vì bạn không chứng thực.

theo trang này salt.netapi.rest_cherrypy trước tiên bạn phải yêu cầu đăng nhập URL và nhận mã thông báo truy cập và sau đó bạn có thể truy cập các chức năng khác thông qua mã thông báo này.

Tôi sẽ giải thích thêm nếu bạn cần.

EDIT: nhiều giải thích: Yêu cầu

Ví dụ qua curl:

curl -si localhost:8000/login \ 
-c ~/cookies.txt \ 
-H "Accept: application/json" \ 
-H "Content-type: application/json" \ 
-d '{ 
    "username": "saltuser", 
    "password": "saltuser", 
    "eauth": "auto" 
}' 

và thông qua lệnh curl này bạn gửi yêu cầu này

POST/HTTP/1.1 
Host: localhost:8000 
Content-Length: 42 
Content-Type: application/json 
Accept: application/json 

{"username": "saltuser", "password": "saltuser", "eauth": "auto"} 

và để đáp ứng bạn nhận được

HTTP/1.1 200 OK 
Content-Type: application/json 
Content-Length: 206 
X-Auth-Token: 6d1b722e 
Set-Cookie: session_id=6d1b722e; expires=Sat, 17 Nov 2012 03:23:52 GMT; Path=/ 

{"return": { 
"token": "6d1b722e", 
"start": 1363805943.776223, 
"expire": 1363849143.776224, 
"user": "saltuser", 
"eauth": "pam", 
"perms": [ 
    "grains.*", 
    "status.*", 
    "sys.*", 
    "test.*" 
] 
}} 

và bạn có thể xem mã thông báo trong nó "token": "6d1b722e"

bây giờ bạn có thể gửi yêu cầu của bạn chứa thẻ giải thích một cây cung như Auth-Mã.

EDIT 2:

giữ trong tâm trí của bạn mà bạn sử dụng pam để xác thực và điều này có nghĩa là bạn cần phải có cùng một người dùng trong os của bạn EDIT 3:

và không làm việc sử dụng con nhỏ này như salt-api conf

external_auth: 
    pam: 
     saltuser: 
     - .* 

    rest_cherrypy: 
    port: 8000 
    disable_ssl: True 
    host: 0.0.0.0 
+0

Vui lòng giải thích thêm! – cybertextron

+0

@cybertextron tôi đã thêm giải thích thêm – sahama

+0

có hoạt động cho người dùng từ xa không? Bạn đã thử làm điều đó từ một máy chủ từ xa? – cybertextron

4

Tôi gặp phải vấn đề tương tự mặc dù sử dụng điểm cuối đăng nhập như được giải thích trong câu trả lời của sahama. Tôi đã giải quyết nó bằng cách đặt rõ ràng "eauth": "pam". Đây là yêu cầu của tôi trông như thế nào bây giờ:

curl -si localhost:8000/login \ 
-c ~/cookies.txt \ 
-H "Accept: application/json" \ 
-H "Content-type: application/json" \ 
-d '{ 
    "username": "saltuser", 
    "password": "saltuser", 
    "eauth": "pam" 
}' 
Các vấn đề liên quan