mirror of https://github.com/jumpserver/jumpserver
perf: Add demo code docs
parent
7d1a7a80fa
commit
e2f6e99514
|
@ -1,8 +1,6 @@
|
||||||
import os
|
import os
|
||||||
|
from django.utils.translation import gettext_lazy as _, get_language
|
||||||
from django.utils.translation import gettext_lazy as _
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils import translation
|
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
|
@ -34,16 +32,25 @@ class IntegrationApplicationViewSet(OrgBulkModelViewSet):
|
||||||
permission_classes=[IsValidUser]
|
permission_classes=[IsValidUser]
|
||||||
)
|
)
|
||||||
def get_sdks_info(self, request, *args, **kwargs):
|
def get_sdks_info(self, request, *args, **kwargs):
|
||||||
readme = ''
|
code_suffix_mapper = {
|
||||||
sdk_language = self.request.query_params.get('language', 'python')
|
'python': 'py',
|
||||||
filename = f'readme.{translation.get_language()}.md'
|
'java': 'java',
|
||||||
readme_path = os.path.join(
|
'go': 'go',
|
||||||
settings.APPS_DIR, 'accounts', 'demos', sdk_language, filename
|
'javascript': 'js',
|
||||||
)
|
'php': 'php',
|
||||||
if os.path.exists(readme_path):
|
}
|
||||||
with open(readme_path, 'r') as f:
|
sdk_language = request.query_params.get('language','python')
|
||||||
readme = f.read()
|
sdk_path = os.path.join(settings.APPS_DIR, 'accounts', 'demos', sdk_language)
|
||||||
return Response(data={'readme': readme})
|
readme_path = os.path.join(sdk_path, f'readme.{get_language()}.md')
|
||||||
|
demo_path = os.path.join(sdk_path, f'demo.{code_suffix_mapper[sdk_language]}')
|
||||||
|
|
||||||
|
def read_file(path):
|
||||||
|
if os.path.exists(path):
|
||||||
|
with open(path, 'r', encoding='utf-8') as f:
|
||||||
|
return f.read()
|
||||||
|
return ''
|
||||||
|
|
||||||
|
return Response(data={'readme': read_file(readme_path), 'code': read_file(demo_path)})
|
||||||
|
|
||||||
@action(
|
@action(
|
||||||
['GET'], detail=True, url_path='secret',
|
['GET'], detail=True, url_path='secret',
|
||||||
|
|
|
@ -1,121 +1,45 @@
|
||||||
```go
|
# 使用说明
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
## 1. 简介
|
||||||
"crypto/hmac"
|
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type APIClient struct {
|
本 API 提供了 PAM 查看资产账号服务,支持 RESTful 风格的调用,返回数据采用 JSON 格式。
|
||||||
Client *http.Client
|
|
||||||
APIURL string
|
|
||||||
KeyID string
|
|
||||||
KeySecret string
|
|
||||||
OrgID string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewAPIClient() *APIClient {
|
## 2. 环境要求
|
||||||
return &APIClient{
|
|
||||||
Client: &http.Client{},
|
|
||||||
APIURL: getEnv("API_URL", "http://127.0.0.1:8080"),
|
|
||||||
KeyID: getEnv("API_KEY_ID", "72b0b0aa-ad82-4182-a631-ae4865e8ae0e"),
|
|
||||||
KeySecret: getEnv("API_KEY_SECRET", "6fuSO7P1m4cj8SSlgaYdblOjNAmnxDVD7tr8"),
|
|
||||||
OrgID: getEnv("ORG_ID", "00000000-0000-0000-0000-000000000002"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getEnv(key, defaultValue string) string {
|
- `Go 1.16+`
|
||||||
value := os.Getenv(key)
|
- `crypto/hmac`
|
||||||
if value == "" {
|
- `crypto/sha256`
|
||||||
return defaultValue
|
- `encoding/base64`
|
||||||
}
|
- `net/http`
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *APIClient) GetAccountSecret(asset, account string) (map[string]interface{}, error) {
|
## 3. 使用方法
|
||||||
u, err := url.Parse(c.APIURL)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse API URL: %v", err)
|
|
||||||
}
|
|
||||||
u.Path = "/api/v1/accounts/integration-applications/account-secret/"
|
|
||||||
|
|
||||||
q := u.Query()
|
**请求方式**: `GET api/v1/accounts/integration-applications/account-secret/`
|
||||||
q.Add("asset", asset)
|
|
||||||
q.Add("account", account)
|
|
||||||
u.RawQuery = q.Encode()
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", u.String(), nil)
|
**请求参数**
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to create request: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
date := time.Now().UTC().Format("Mon, 02 Jan 2006 15:04:05 GMT")
|
| 参数名 | 类型 | 必填 | 说明 |
|
||||||
req.Header.Set("Accept", "application/json")
|
|----------|------|-----|---------------|
|
||||||
req.Header.Set("X-JMS-ORG", c.OrgID)
|
| asset | str | 是 | 资产 ID / 资产名称 |
|
||||||
req.Header.Set("Date", date)
|
| account | str | 是 | 账号 ID / 账号名称 |
|
||||||
req.Header.Set("X-Source", "jms-pam")
|
|
||||||
|
|
||||||
headersList := []string{"(request-target)", "accept", "date", "x-jms-org"}
|
**响应示例**:
|
||||||
var signatureParts []string
|
```json
|
||||||
|
{
|
||||||
for _, h := range headersList {
|
"id": "72b0b0aa-ad82-4182-a631-ae4865e8ae0e",
|
||||||
var value string
|
"secret": "123456"
|
||||||
if h == "(request-target)" {
|
|
||||||
value = strings.ToLower(req.Method) + " " + req.URL.RequestURI()
|
|
||||||
} else {
|
|
||||||
canonicalKey := http.CanonicalHeaderKey(h)
|
|
||||||
value = req.Header.Get(canonicalKey)
|
|
||||||
}
|
|
||||||
signatureParts = append(signatureParts, fmt.Sprintf("%s: %s", h, value))
|
|
||||||
}
|
|
||||||
|
|
||||||
signatureString := strings.Join(signatureParts, "\n")
|
|
||||||
mac := hmac.New(sha256.New, []byte(c.KeySecret))
|
|
||||||
mac.Write([]byte(signatureString))
|
|
||||||
signatureB64 := base64.StdEncoding.EncodeToString(mac.Sum(nil))
|
|
||||||
|
|
||||||
headersJoined := strings.Join(headersList, " ")
|
|
||||||
authHeader := fmt.Sprintf(
|
|
||||||
`Signature keyId="%s",algorithm="hmac-sha256",headers="%s",signature="%s"`,
|
|
||||||
c.KeyID,
|
|
||||||
headersJoined,
|
|
||||||
signatureB64,
|
|
||||||
)
|
|
||||||
req.Header.Set("Authorization", authHeader)
|
|
||||||
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("request failed: %v", err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return nil, fmt.Errorf("API returned non-200 status: %d", resp.StatusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
var result map[string]interface{}
|
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to decode response: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
client := NewAPIClient()
|
|
||||||
result, err := client.GetAccountSecret("ubuntu_docker", "root")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error: %v", err)
|
|
||||||
}
|
|
||||||
fmt.Printf("Result: %+v\n", result)
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 常见问题(FAQ)
|
||||||
|
|
||||||
|
Q: API Key 如何获取?
|
||||||
|
|
||||||
|
A: 你可以在 PAM - 应用管理 创建应用生成 KEY_ID 和 KEY_SECRET。
|
||||||
|
|
||||||
|
## 版本历史(Changelog)
|
||||||
|
|
||||||
|
|
||||||
|
| 版本号 | 变更内容 | 日期 |
|
||||||
|
| ----- | ----------------- |------------|
|
||||||
|
| 1.0.0 | 初始版本 | 2025-02-11 |
|
|
@ -0,0 +1,119 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type APIClient struct {
|
||||||
|
Client *http.Client
|
||||||
|
APIURL string
|
||||||
|
KeyID string
|
||||||
|
KeySecret string
|
||||||
|
OrgID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAPIClient() *APIClient {
|
||||||
|
return &APIClient{
|
||||||
|
Client: &http.Client{},
|
||||||
|
APIURL: getEnv("API_URL", "http://127.0.0.1:8080"),
|
||||||
|
KeyID: getEnv("API_KEY_ID", "72b0b0aa-ad82-4182-a631-ae4865e8ae0e"),
|
||||||
|
KeySecret: getEnv("API_KEY_SECRET", "6fuSO7P1m4cj8SSlgaYdblOjNAmnxDVD7tr8"),
|
||||||
|
OrgID: getEnv("ORG_ID", "00000000-0000-0000-0000-000000000002"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEnv(key, defaultValue string) string {
|
||||||
|
value := os.Getenv(key)
|
||||||
|
if value == "" {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *APIClient) GetAccountSecret(asset, account string) (map[string]interface{}, error) {
|
||||||
|
u, err := url.Parse(c.APIURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse API URL: %v", err)
|
||||||
|
}
|
||||||
|
u.Path = "/api/v1/accounts/integration-applications/account-secret/"
|
||||||
|
|
||||||
|
q := u.Query()
|
||||||
|
q.Add("asset", asset)
|
||||||
|
q.Add("account", account)
|
||||||
|
u.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", u.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
date := time.Now().UTC().Format("Mon, 02 Jan 2006 15:04:05 GMT")
|
||||||
|
req.Header.Set("Accept", "application/json")
|
||||||
|
req.Header.Set("X-JMS-ORG", c.OrgID)
|
||||||
|
req.Header.Set("Date", date)
|
||||||
|
req.Header.Set("X-Source", "jms-pam")
|
||||||
|
|
||||||
|
headersList := []string{"(request-target)", "accept", "date", "x-jms-org"}
|
||||||
|
var signatureParts []string
|
||||||
|
|
||||||
|
for _, h := range headersList {
|
||||||
|
var value string
|
||||||
|
if h == "(request-target)" {
|
||||||
|
value = strings.ToLower(req.Method) + " " + req.URL.RequestURI()
|
||||||
|
} else {
|
||||||
|
canonicalKey := http.CanonicalHeaderKey(h)
|
||||||
|
value = req.Header.Get(canonicalKey)
|
||||||
|
}
|
||||||
|
signatureParts = append(signatureParts, fmt.Sprintf("%s: %s", h, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
signatureString := strings.Join(signatureParts, "\n")
|
||||||
|
mac := hmac.New(sha256.New, []byte(c.KeySecret))
|
||||||
|
mac.Write([]byte(signatureString))
|
||||||
|
signatureB64 := base64.StdEncoding.EncodeToString(mac.Sum(nil))
|
||||||
|
|
||||||
|
headersJoined := strings.Join(headersList, " ")
|
||||||
|
authHeader := fmt.Sprintf(
|
||||||
|
`Signature keyId="%s",algorithm="hmac-sha256",headers="%s",signature="%s"`,
|
||||||
|
c.KeyID,
|
||||||
|
headersJoined,
|
||||||
|
signatureB64,
|
||||||
|
)
|
||||||
|
req.Header.Set("Authorization", authHeader)
|
||||||
|
|
||||||
|
resp, err := c.Client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("request failed: %v", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("API returned non-200 status: %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
var result map[string]interface{}
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to decode response: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
client := NewAPIClient()
|
||||||
|
result, err := client.GetAccountSecret("ubuntu_docker", "root")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error: %v", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Result: %+v\n", result)
|
||||||
|
}
|
|
@ -1,45 +1,42 @@
|
||||||
```python
|
# 使用说明
|
||||||
import requests
|
|
||||||
import os
|
|
||||||
from datetime import datetime
|
|
||||||
from httpsig.requests_auth import HTTPSignatureAuth
|
|
||||||
|
|
||||||
API_URL = os.getenv("API_URL", "http://127.0.0.1:8080")
|
## 1. 简介
|
||||||
KEY_ID = os.getenv("API_KEY_ID", "72b0b0aa-ad82-4182-a631-ae4865e8ae0e")
|
|
||||||
KEY_SECRET = os.getenv("API_KEY_SECRET", "6fuSO7P1m4cj8SSlgaYdblOjNAmnxDVD7tr8")
|
|
||||||
ORG_ID = os.getenv("ORG_ID", "00000000-0000-0000-0000-000000000002")
|
|
||||||
|
|
||||||
|
本 API 提供了 PAM 查看资产账号服务,支持 RESTful 风格的调用,返回数据采用 JSON 格式。
|
||||||
|
|
||||||
class APIClient:
|
## 2. 环境要求
|
||||||
def __init__(self):
|
|
||||||
self.session = requests.Session()
|
|
||||||
self.auth = HTTPSignatureAuth(
|
|
||||||
key_id=KEY_ID, secret=KEY_SECRET,
|
|
||||||
algorithm='hmac-sha256', headers=['(request-target)', 'accept', 'date', 'x-jms-org']
|
|
||||||
)
|
|
||||||
|
|
||||||
def get_account_secret(self, asset, account):
|
- `Python 3.11+`
|
||||||
url = f"{API_URL}/api/v1/accounts/integration-applications/account-secret/"
|
- `requests==2.31.0`
|
||||||
headers = {
|
- `httpsig==1.3.0`
|
||||||
'Accept': 'application/json',
|
|
||||||
'X-JMS-ORG': ORG_ID,
|
## 3. 使用方法
|
||||||
'Date': datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT'),
|
**请求方式**: `GET api/v1/accounts/integration-applications/account-secret/`
|
||||||
'X-Source': 'jms-pam'
|
|
||||||
|
**请求参数**
|
||||||
|
|
||||||
|
| 参数名 | 类型 | 必填 | 说明 |
|
||||||
|
|------------|------|----|--------------|
|
||||||
|
| asset | str | 是 | 资产 ID / 资产名称 |
|
||||||
|
| account | str | 是 | 账号 ID / 账号名称 |
|
||||||
|
|
||||||
|
**响应示例**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "72b0b0aa-ad82-4182-a631-ae4865e8ae0e",
|
||||||
|
"secret": "123456"
|
||||||
}
|
}
|
||||||
params = {"asset": asset, "account": account}
|
|
||||||
|
|
||||||
try:
|
|
||||||
response = self.session.get(url, auth=self.auth, headers=headers, params=params, timeout=10)
|
|
||||||
response.raise_for_status()
|
|
||||||
return response.json()
|
|
||||||
except requests.RequestException as e:
|
|
||||||
print(f"API 请求失败: {e}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
# 示例调用
|
|
||||||
if __name__ == "__main__":
|
|
||||||
client = APIClient()
|
|
||||||
result = client.get_account_secret(asset="ubuntu_docker", account="root")
|
|
||||||
print(result)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 常见问题(FAQ)
|
||||||
|
|
||||||
|
Q: API Key 如何获取?
|
||||||
|
|
||||||
|
A: 你可以在 PAM - 应用管理 创建应用生成 KEY_ID 和 KEY_SECRET。
|
||||||
|
|
||||||
|
## 版本历史(Changelog)
|
||||||
|
|
||||||
|
|
||||||
|
| 版本号 | 变更内容 | 日期 |
|
||||||
|
| ----- | ----------------- |------------|
|
||||||
|
| 1.0.0 | 初始版本 | 2025-02-11 |
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
# 示例调用
|
||||||
|
|
||||||
|
import requests
|
||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
from httpsig.requests_auth import HTTPSignatureAuth
|
||||||
|
|
||||||
|
API_URL = os.getenv("API_URL", "http://127.0.0.1:8080")
|
||||||
|
KEY_ID = os.getenv("API_KEY_ID", "72b0b0aa-ad82-4182-a631-ae4865e8ae0e")
|
||||||
|
KEY_SECRET = os.getenv("API_KEY_SECRET", "6fuSO7P1m4cj8SSlgaYdblOjNAmnxDVD7tr8")
|
||||||
|
ORG_ID = os.getenv("ORG_ID", "00000000-0000-0000-0000-000000000002")
|
||||||
|
|
||||||
|
|
||||||
|
class APIClient:
|
||||||
|
def __init__(self):
|
||||||
|
self.session = requests.Session()
|
||||||
|
self.auth = HTTPSignatureAuth(
|
||||||
|
key_id=KEY_ID, secret=KEY_SECRET,
|
||||||
|
algorithm='hmac-sha256', headers=['(request-target)', 'accept', 'date', 'x-jms-org']
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_account_secret(self, asset, account):
|
||||||
|
url = f"{API_URL}/api/v1/accounts/integration-applications/account-secret/"
|
||||||
|
headers = {
|
||||||
|
'Accept': 'application/json',
|
||||||
|
'X-JMS-ORG': ORG_ID,
|
||||||
|
'Date': datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT'),
|
||||||
|
'X-Source': 'jms-pam'
|
||||||
|
}
|
||||||
|
params = {"asset": asset, "account": account}
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = self.session.get(url, auth=self.auth, headers=headers, params=params, timeout=10)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()
|
||||||
|
except requests.RequestException as e:
|
||||||
|
print(f"API 请求失败: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
client = APIClient()
|
||||||
|
result = client.get_account_secret(asset="ubuntu_docker", account="root")
|
||||||
|
print(result)
|
|
@ -351,6 +351,7 @@
|
||||||
"ConvenientOperate": "Convenient action",
|
"ConvenientOperate": "Convenient action",
|
||||||
"Copy": "Copy",
|
"Copy": "Copy",
|
||||||
"CopySuccess": "Copy successful",
|
"CopySuccess": "Copy successful",
|
||||||
|
"CopyFailed": "Copy failed",
|
||||||
"Corporation": "Company",
|
"Corporation": "Company",
|
||||||
"Create": "Create",
|
"Create": "Create",
|
||||||
"CreateAccessKey": "Create access key",
|
"CreateAccessKey": "Create access key",
|
||||||
|
|
|
@ -347,6 +347,7 @@
|
||||||
"ConvenientOperate": "便捷操作",
|
"ConvenientOperate": "便捷操作",
|
||||||
"Copy": "复制",
|
"Copy": "复制",
|
||||||
"CopySuccess": "复制成功",
|
"CopySuccess": "复制成功",
|
||||||
|
"CopyFailed": "复制失败",
|
||||||
"Corporation": "公司",
|
"Corporation": "公司",
|
||||||
"Create": "创建",
|
"Create": "创建",
|
||||||
"CreateAccessKey": "创建访问密钥",
|
"CreateAccessKey": "创建访问密钥",
|
||||||
|
|
Loading…
Reference in New Issue