Skip to content

Commit feec750

Browse files
committed
Support Bearer token authentication and test.
1 parent c513663 commit feec750

40 files changed

+2237
-499
lines changed

.github/workflows/test.yml

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,48 @@ jobs:
6868
docker tag platform:latest ossrs/srs-cloud:$SRS_TAG &&
6969
docker tag platform:latest registry.cn-hangzhou.aliyuncs.com/ossrs/srs-cloud:$SRS_TAG &&
7070
docker images
71-
- name: Test build install package
71+
- name: Build package
7272
run: |
7373
bash scripts/setup-ubuntu/build.sh --language zh --version $SRS_TAG \
74-
--output build --extract
74+
--output build --extract
7575
du -sh build/*
76-
- name: Test install package
76+
- name: Install package
7777
run: |
7878
sudo bash build/srs-cloud/scripts/setup-ubuntu/install.sh --verbose &&
7979
du -sh /usr/local/srs-cloud/*
80-
- name: Check installed package
80+
- name: Check service
8181
run: |
82+
# Wait for service ready.
83+
make -j -C test
84+
./test/srs-cloud.test -test.v -srs-log -endpoint http://localhost:2022 \
85+
-wait-ready -check-api-secret=false \
86+
-test.run TestApi_Empty
87+
88+
echo "Record log of services."
89+
docker ps -a
8290
systemctl status srs-cloud
91+
journalctl -u srs-cloud -f >journalctl.log 2>&1 & pid_journalctl=$!
92+
docker logs -f srs-cloud >docker.log 2>&1 & pid_docker=$!
93+
- name: Test service
94+
run: |
95+
# We will handle the error by ourselves.
96+
set +e
97+
98+
SRS_PLATFORM_SECRET=$(docker exec srs-cloud redis-cli hget SRS_PLATFORM_SECRET token)
99+
./test/srs-cloud.test -test.v -wait-ready -srs-log -endpoint http://localhost:2022 \
100+
-wait-ready -check-api-secret=true -api-secret=$SRS_PLATFORM_SECRET \
101+
-init-password
102+
ret=$?; echo "Test with ${SRS_PLATFORM_SECRET} result: $ret"
103+
104+
echo "Stop service"
105+
sudo systemctl stop srs-cloud
106+
kill $pid_journalctl 2>/dev/null
107+
kill $pid_docker 2>/dev/null
108+
109+
echo "Log of journalctl.log" && cat journalctl.log
110+
echo "Log of docker.log" && cat docker.log
111+
112+
exit $ret
83113
84114
final:
85115
name: Final

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,4 @@ t.md
131131
/build
132132
/*.tar
133133
/srs-cloud2
134+
/test/srs-cloud.test

DEVELOPER.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ Platform:
221221
* `/terraform/v1/mgmt/beian/query` Query the beian information.
222222
* `/terraform/v1/mgmt/beian/update` Update the beian information.
223223
* `/terraform/v1/mgmt/secret/query` Query the api secret for OpenAPI.
224-
* `/terraform/v1/mgmt/secret/token` Create token for OpenAPI.
225224
* `/terraform/v1/mgmt/nginx/hls` Update NGINX config, to enable HLS delivery.
226225
* `/terraform/v1/host/versions` Public version api.
227226
* `/terraform/v1/releases` Version management for all components.
@@ -282,6 +281,7 @@ Also provided by platform for static Files:
282281
* `/terraform/v1/mgmt/upgrade` Upgrade the mgmt to latest version.
283282
* `/terraform/v1/mgmt/containers` Query SRS container.
284283
* `/terraform/v1/host/exec` Exec command sync, response the stdout and stderr.
284+
* `/terraform/v1/mgmt/secret/token` Create token for OpenAPI.
285285

286286
## Depends
287287

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ADD mgmt /g/mgmt
1818
ADD platform /g/platform
1919
ADD ui /g/ui
2020
ADD usr /g/usr
21+
ADD test /g/test
2122
ADD Makefile /g/Makefile
2223

2324
# Note that we only build the platform without ui, because already build ui for all OS.

Dockerfile.script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ FROM ${ARCH}jrei/systemd-ubuntu:focal AS dist
2222
ENV DEBIAN_FRONTEND=noninteractive
2323

2424
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#apt-get
25-
RUN apt update -y && apt-get install -y docker.io
25+
RUN apt update -y && apt-get install -y docker.io make
2626

2727
# Copy nodejs for ui build.
2828
COPY --from=node /usr/local/bin /usr/local/bin

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ __REAL_INSTALL = $(DESTDIR)$(PREFIX)
66
default: build
77

88
help:
9-
@echo "Usage: make build|install|test"
9+
@echo "Usage: make build|install|utest"
1010
@echo " default Show help and quit"
1111
@echo " build Build the project, npm install and build the ui"
1212
@echo " install Copy files for installer"
@@ -15,11 +15,13 @@ help:
1515
build:
1616
make -C platform
1717
make -C ui
18+
make -C test
1819
make -C releases
1920

2021
clean:
2122
make -C platform clean
2223
make -C ui clean
24+
make -C test clean
2325
make -C releases clean
2426

2527
install:
@@ -48,6 +50,7 @@ else
4850
endif
4951

5052
test:
51-
cd platform && go test ./...
52-
cd releases && go test ./...
53+
cd platform && go test -v ./...
54+
cd releases && go test -v ./...
55+
cd test && go test -v -check-api-secret=false -test.run TestApi_Empty ./...
5356
cd ui && npm run test

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ You have the option to modify the volumes for srs-cloud and direct them to diffe
2727

2828
* `/data` The global data directory.
2929
* `redis` The redis data directory, the publish secret and record configuration.
30-
* `srs-cloud` The data directory for srs-cloud
31-
* `config` The mgmt password and cloud configuration.
32-
* `record` The record storage directory, save record files.
33-
* `vlive` The storage directory for virtual live, save video files.
30+
* `config` The mgmt password and cloud configuration.
31+
* `record` The record storage directory, save record files.
32+
* `vlive` The storage directory for virtual live, save video files.
3433

3534
You can use environment variables to modify the settings.
3635

mgmt/bootstrap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ echo "Start platform container"
3030
CMD="docker run -v ${DATA_HOME}:/data ${EXTRA_PARAMS}
3131
-p ${MGMT_PORT}:2022 -p ${RTMP_PORT}:1935/tcp -p ${API_PORT}:1985/tcp -p
3232
${HTTP_PORT}:8080/tcp -p ${RTC_PORT}:8000/udp -p ${SRT_PORT}:10080/udp
33+
--log-driver=json-file --log-opt=max-size=1g --log-opt=max-file=3
3334
--restart no --rm -it --name srs-cloud --detach
3435
${IMAGE}"
3536
echo $CMD && $CMD

platform/dvr-local-disk.go

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"encoding/json"
1111
"fmt"
1212
"io"
13-
"io/ioutil"
1413
"net/http"
1514
"os"
1615
"os/exec"
@@ -25,7 +24,6 @@ import (
2524

2625
// Use v8 because we use Go 1.16+, while v9 requires Go 1.18+
2726
"github.com/go-redis/redis/v8"
28-
"github.com/golang-jwt/jwt/v4"
2927
"github.com/google/uuid"
3028
)
3129

@@ -52,27 +50,18 @@ func (v *RecordWorker) Handle(ctx context.Context, handler *http.ServeMux) error
5250
logger.Tf(ctx, "Handle %v", ep)
5351
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
5452
if err := func() error {
55-
b, err := ioutil.ReadAll(r.Body)
56-
if err != nil {
57-
return errors.Wrapf(err, "read body")
58-
}
59-
6053
var token string
61-
if err := json.Unmarshal(b, &struct {
54+
if err := ParseBody(ctx, r.Body, &struct {
6255
Token *string `json:"token"`
6356
}{
6457
Token: &token,
6558
}); err != nil {
66-
return errors.Wrapf(err, "json unmarshal %v", string(b))
59+
return errors.Wrapf(err, "parse body")
6760
}
6861

6962
apiSecret := os.Getenv("SRS_PLATFORM_SECRET")
70-
// Verify token first, @see https://www.npmjs.com/package/jsonwebtoken#errors--codes
71-
// See https://pkg.go.dev/github.com/golang-jwt/jwt/v4#example-Parse-Hmac
72-
if _, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
73-
return []byte(apiSecret), nil
74-
}); err != nil {
75-
return errors.Wrapf(err, "verify token %v", token)
63+
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
64+
return errors.Wrapf(err, "authenticate")
7665
}
7766

7867
if all, err := rdb.HGet(ctx, SRS_RECORD_PATTERNS, "all").Result(); err != nil && err != redis.Nil {
@@ -97,29 +86,20 @@ func (v *RecordWorker) Handle(ctx context.Context, handler *http.ServeMux) error
9786
logger.Tf(ctx, "Handle %v", ep)
9887
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
9988
if err := func() error {
100-
b, err := ioutil.ReadAll(r.Body)
101-
if err != nil {
102-
return errors.Wrapf(err, "read body")
103-
}
104-
10589
var token string
10690
var all bool
107-
if err := json.Unmarshal(b, &struct {
91+
if err := ParseBody(ctx, r.Body, &struct {
10892
Token *string `json:"token"`
10993
All *bool `json:"all"`
11094
}{
11195
Token: &token, All: &all,
11296
}); err != nil {
113-
return errors.Wrapf(err, "json unmarshal %v", string(b))
97+
return errors.Wrapf(err, "parse body")
11498
}
11599

116100
apiSecret := os.Getenv("SRS_PLATFORM_SECRET")
117-
// Verify token first, @see https://www.npmjs.com/package/jsonwebtoken#errors--codes
118-
// See https://pkg.go.dev/github.com/golang-jwt/jwt/v4#example-Parse-Hmac
119-
if _, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
120-
return []byte(apiSecret), nil
121-
}); err != nil {
122-
return errors.Wrapf(err, "verify token %v", token)
101+
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
102+
return errors.Wrapf(err, "authenticate")
123103
}
124104

125105
if all, err := rdb.HSet(ctx, SRS_RECORD_PATTERNS, "all", fmt.Sprintf("%v", all)).Result(); err != nil && err != redis.Nil {
@@ -138,31 +118,23 @@ func (v *RecordWorker) Handle(ctx context.Context, handler *http.ServeMux) error
138118
logger.Tf(ctx, "Handle %v", ep)
139119
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
140120
if err := func() error {
141-
b, err := ioutil.ReadAll(r.Body)
142-
if err != nil {
143-
return errors.Wrapf(err, "read body")
144-
}
145-
146121
var token, uuid string
147-
if err := json.Unmarshal(b, &struct {
122+
if err := ParseBody(ctx, r.Body, &struct {
148123
Token *string `json:"token"`
149124
UUID *string `json:"uuid"`
150125
}{
151126
Token: &token, UUID: &uuid,
152127
}); err != nil {
153-
return errors.Wrapf(err, "json unmarshal %v", string(b))
154-
}
155-
if uuid == "" {
156-
return errors.New("no uuid")
128+
return errors.Wrapf(err, "parse body")
157129
}
158130

159131
apiSecret := os.Getenv("SRS_PLATFORM_SECRET")
160-
// Verify token first, @see https://www.npmjs.com/package/jsonwebtoken#errors--codes
161-
// See https://pkg.go.dev/github.com/golang-jwt/jwt/v4#example-Parse-Hmac
162-
if _, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
163-
return []byte(apiSecret), nil
164-
}); err != nil {
165-
return errors.Wrapf(err, "verify token %v", token)
132+
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
133+
return errors.Wrapf(err, "authenticate")
134+
}
135+
136+
if uuid == "" {
137+
return errors.New("no uuid")
166138
}
167139

168140
var metadata M3u8VoDArtifact
@@ -216,27 +188,18 @@ func (v *RecordWorker) Handle(ctx context.Context, handler *http.ServeMux) error
216188
logger.Tf(ctx, "Handle %v", ep)
217189
handler.HandleFunc(ep, func(w http.ResponseWriter, r *http.Request) {
218190
if err := func() error {
219-
b, err := ioutil.ReadAll(r.Body)
220-
if err != nil {
221-
return errors.Wrapf(err, "read body")
222-
}
223-
224191
var token string
225-
if err := json.Unmarshal(b, &struct {
192+
if err := ParseBody(ctx, r.Body, &struct {
226193
Token *string `json:"token"`
227194
}{
228195
Token: &token,
229196
}); err != nil {
230-
return errors.Wrapf(err, "json unmarshal %v", string(b))
197+
return errors.Wrapf(err, "parse body")
231198
}
232199

233200
apiSecret := os.Getenv("SRS_PLATFORM_SECRET")
234-
// Verify token first, @see https://www.npmjs.com/package/jsonwebtoken#errors--codes
235-
// See https://pkg.go.dev/github.com/golang-jwt/jwt/v4#example-Parse-Hmac
236-
if _, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
237-
return []byte(apiSecret), nil
238-
}); err != nil {
239-
return errors.Wrapf(err, "verify token %v", token)
201+
if err := Authenticate(ctx, apiSecret, token, r.Header); err != nil {
202+
return errors.Wrapf(err, "authenticate")
240203
}
241204

242205
keys, cursor, err := rdb.HScan(ctx, SRS_RECORD_M3U8_ARTIFACT, 0, "*", 100).Result()

0 commit comments

Comments
 (0)