Skip to content

Commit 00de63a

Browse files
authored
Merge pull request #851 from Pangjiping/fix/go-sdk-quality
fix(go-sdk): drain response bodies, add io.Closer, remove deprecated DSA
2 parents 05d055c + 59ef760 commit 00de63a

9 files changed

Lines changed: 265 additions & 68 deletions

File tree

sdks/sandbox/go/crypto_policy.go

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package opensandbox
1616

1717
import (
18-
"crypto/dsa"
1918
"crypto/ecdsa"
2019
"crypto/ed25519"
2120
"crypto/rsa"
@@ -25,20 +24,18 @@ import (
2524
)
2625

2726
const (
28-
nistMinRSABits = 2048
29-
nistMinDLKeyBits = 224
30-
nistMinDLGroupBits = 2048
31-
nistMinECBits = 224
32-
nistMinHashBits = 224
27+
nistMinRSABits = 2048
28+
nistMinECBits = 224
29+
nistMinHashBits = 224
3330
)
3431

3532
func minHashBitsForSignatureAlgorithm(algo x509.SignatureAlgorithm) (int, error) {
3633
switch algo {
3734
case x509.MD2WithRSA, x509.MD5WithRSA:
3835
return 128, nil
39-
case x509.SHA1WithRSA, x509.DSAWithSHA1, x509.ECDSAWithSHA1:
36+
case x509.SHA1WithRSA, x509.ECDSAWithSHA1:
4037
return 160, nil
41-
case x509.DSAWithSHA256, x509.SHA256WithRSA, x509.ECDSAWithSHA256:
38+
case x509.SHA256WithRSA, x509.ECDSAWithSHA256:
4239
return 256, nil
4340
case x509.SHA384WithRSA, x509.ECDSAWithSHA384:
4441
return 384, nil
@@ -103,26 +100,6 @@ func ensureCertPublicKeyMeetsNISTMinimums(cert *x509.Certificate) error {
103100
nistMinECBits,
104101
)
105102
}
106-
case *dsa.PublicKey:
107-
if pub.Parameters.P == nil || pub.Parameters.Q == nil {
108-
return fmt.Errorf("certificate DSA public key parameters are incomplete")
109-
}
110-
subgroupBits := pub.Parameters.Q.BitLen()
111-
groupBits := pub.Parameters.P.BitLen()
112-
if subgroupBits < nistMinDLKeyBits {
113-
return fmt.Errorf(
114-
"certificate DSA subgroup (Q) length %d bits is below NIST minimum %d bits",
115-
subgroupBits,
116-
nistMinDLKeyBits,
117-
)
118-
}
119-
if groupBits < nistMinDLGroupBits {
120-
return fmt.Errorf(
121-
"certificate DSA group (P) length %d bits is below NIST minimum %d bits",
122-
groupBits,
123-
nistMinDLGroupBits,
124-
)
125-
}
126103
case ed25519.PublicKey:
127104
bits := len(pub) * 8
128105
if bits < nistMinECBits {

sdks/sandbox/go/execd.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ func (e *ExecdClient) Ping(ctx context.Context) error {
5151
// ListContexts returns all active code execution contexts for the given language.
5252
func (e *ExecdClient) ListContexts(ctx context.Context, language string) ([]CodeContext, error) {
5353
var result []CodeContext
54-
path := "/code/contexts?language=" + url.QueryEscape(language)
54+
params := url.Values{}
55+
params.Set("language", language)
56+
path := "/code/contexts?" + params.Encode()
5557
err := e.client.doRequest(ctx, http.MethodGet, path, nil, &result)
5658
return result, err
5759
}
@@ -85,7 +87,9 @@ func (e *ExecdClient) DeleteContext(ctx context.Context, contextID string) error
8587

8688
// DeleteContextsByLanguage deletes all code execution contexts for the given language.
8789
func (e *ExecdClient) DeleteContextsByLanguage(ctx context.Context, language string) error {
88-
path := "/code/contexts?language=" + url.QueryEscape(language)
90+
params := url.Values{}
91+
params.Set("language", language)
92+
path := "/code/contexts?" + params.Encode()
8993
return e.client.doRequest(ctx, http.MethodDelete, path, nil, nil)
9094
}
9195

@@ -97,7 +101,9 @@ func (e *ExecdClient) ExecuteCode(ctx context.Context, req RunCodeRequest, handl
97101

98102
// InterruptCode interrupts the currently running code execution.
99103
func (e *ExecdClient) InterruptCode(ctx context.Context, sessionID string) error {
100-
path := "/code?id=" + url.QueryEscape(sessionID)
104+
params := url.Values{}
105+
params.Set("id", sessionID)
106+
path := "/code?" + params.Encode()
101107
return e.client.doRequest(ctx, http.MethodDelete, path, nil, nil)
102108
}
103109

@@ -131,7 +137,9 @@ func (e *ExecdClient) RunCommand(ctx context.Context, req RunCommandRequest, han
131137

132138
// InterruptCommand interrupts the currently running command execution.
133139
func (e *ExecdClient) InterruptCommand(ctx context.Context, sessionID string) error {
134-
path := "/command?id=" + url.QueryEscape(sessionID)
140+
params := url.Values{}
141+
params.Set("id", sessionID)
142+
path := "/command?" + params.Encode()
135143
return e.client.doRequest(ctx, http.MethodDelete, path, nil, nil)
136144
}
137145

@@ -207,7 +215,9 @@ func (e *ExecdClient) GetCommandLogs(ctx context.Context, commandID string, curs
207215
// GetFileInfo retrieves metadata for the file at the given path.
208216
func (e *ExecdClient) GetFileInfo(ctx context.Context, path string) (map[string]FileInfo, error) {
209217
var result map[string]FileInfo
210-
reqPath := "/files/info?path=" + url.QueryEscape(path)
218+
params := url.Values{}
219+
params.Set("path", path)
220+
reqPath := "/files/info?" + params.Encode()
211221
err := e.client.doRequest(ctx, http.MethodGet, reqPath, nil, &result)
212222
return result, err
213223
}
@@ -365,7 +375,9 @@ func (e *ExecdClient) newUploadFilesRequest(ctx context.Context, entries []Uploa
365375
// returned io.ReadCloser. Pass rangeHeader (e.g. "bytes=0-1023") for partial
366376
// content, or empty string for the full file.
367377
func (e *ExecdClient) DownloadFile(ctx context.Context, remotePath string, rangeHeader string) (io.ReadCloser, error) {
368-
reqPath := "/files/download?path=" + url.QueryEscape(remotePath)
378+
params := url.Values{}
379+
params.Set("path", remotePath)
380+
reqPath := "/files/download?" + params.Encode()
369381

370382
var resp *http.Response
371383
err := e.client.withRetry(ctx, func() error {
@@ -421,7 +433,9 @@ func OctalMode(m os.FileMode) int {
421433

422434
// DeleteDirectory deletes a directory and all its contents recursively.
423435
func (e *ExecdClient) DeleteDirectory(ctx context.Context, path string) error {
424-
reqPath := "/directories?path=" + url.QueryEscape(path)
436+
params := url.Values{}
437+
params.Set("path", path)
438+
reqPath := "/directories?" + params.Encode()
425439
return e.client.doRequest(ctx, http.MethodDelete, reqPath, nil, nil)
426440
}
427441

sdks/sandbox/go/go.mod

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
11
module github.com/alibaba/OpenSandbox/sdks/sandbox/go
22

33
go 1.20
4-
5-
require github.com/oapi-codegen/runtime v1.2.0
6-
7-
require (
8-
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
9-
github.com/google/uuid v1.6.0 // indirect
10-
github.com/stretchr/testify v1.11.1 // indirect
11-
)

sdks/sandbox/go/go.sum

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +0,0 @@
1-
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
2-
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
3-
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
4-
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
5-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
7-
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
8-
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9-
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
10-
github.com/oapi-codegen/runtime v1.2.0 h1:RvKc1CVS1QeKSNzO97FBQbSMZyQ8s6rZd+LpmzwHMP4=
11-
github.com/oapi-codegen/runtime v1.2.0/go.mod h1:Y7ZhmmlE8ikZOmuHRRndiIm7nf3xcVv+YMweKgG1DT0=
12-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
13-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
14-
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
15-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
16-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
17-
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
18-
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
19-
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

sdks/sandbox/go/http.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,14 @@ func (c *Client) doRequestOnce(ctx context.Context, method, path string, body an
167167

168168
// No content (e.g. 204)
169169
if resp.StatusCode == http.StatusNoContent || result == nil {
170+
io.Copy(io.Discard, resp.Body)
170171
return nil
171172
}
172173

173174
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
174175
return fmt.Errorf("opensandbox: decode response: %w", err)
175176
}
177+
io.Copy(io.Discard, resp.Body)
176178
return nil
177179
}
178180

sdks/sandbox/go/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,4 @@ func (m *SandboxManager) DeleteSnapshot(ctx context.Context, snapshotID string)
8888
}
8989

9090
// Close releases local resources. Currently a no-op placeholder.
91-
func (m *SandboxManager) Close() {}
91+
func (m *SandboxManager) Close() error { return nil }

sdks/sandbox/go/retry_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"crypto/rsa"
2323
"crypto/tls"
2424
"crypto/x509"
25-
"encoding/json"
2625
"fmt"
2726
"math/big"
2827
"net/http"
@@ -637,6 +636,3 @@ func TestRetry_CustomRetryableStatusCodes(t *testing.T) {
637636
require.Equal(t, "sbx-500-retried", got.ID)
638637
require.Equal(t, int32(2), attempts.Load())
639638
}
640-
641-
// suppress unused import warning
642-
var _ = json.Marshal

sdks/sandbox/go/sandbox.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,10 @@ func (s *Sandbox) Kill(ctx context.Context) error {
227227
}
228228

229229
// Close releases local HTTP resources. Does NOT terminate the sandbox.
230-
func (s *Sandbox) Close() {
230+
func (s *Sandbox) Close() error {
231231
// No-op for now — Go's http.Client doesn't need explicit close.
232232
// Placeholder for future transport pooling.
233+
return nil
233234
}
234235

235236
// Pause pauses the sandbox while preserving its state.

0 commit comments

Comments
 (0)