Skip to content

Commit ac01d37

Browse files
committed
reverting changes
1 parent 571989b commit ac01d37

File tree

4 files changed

+26
-112
lines changed

4 files changed

+26
-112
lines changed

apiintegrations/apihandler/apihandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type APIHandler interface {
1414
ConstructAPIResourceEndpoint(endpointPath string, log logger.Logger) string
1515
ConstructAPIAuthEndpoint(endpointPath string, log logger.Logger) string
1616
MarshalRequest(body interface{}, method string, endpoint string, log logger.Logger) ([]byte, error)
17-
MarshalMultipartRequest(fields map[string]string, files map[string]string, log logger.Logger) ([]byte, string, string, error)
17+
MarshalMultipartRequest(fields map[string]string, files map[string]string, log logger.Logger) ([]byte, string, error)
1818
GetContentTypeHeader(method string, log logger.Logger) string
1919
GetAcceptHeader() string
2020
GetDefaultBaseDomain() string

apiintegrations/jamfpro/jamfpro_api_request.go

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,60 +56,43 @@ func (j *JamfAPIHandler) MarshalRequest(body interface{}, method string, endpoin
5656
return data, nil
5757
}
5858

59-
// MarshalMultipartRequest creates a multipart request body for sending files and form fields in a single request.
60-
func (j *JamfAPIHandler) MarshalMultipartRequest(fields map[string]string, files map[string]string, log logger.Logger) ([]byte, string, string, error) {
59+
// MarshalMultipartRequest handles multipart form data encoding with secure file handling and returns the encoded body and content type.
60+
func (j *JamfAPIHandler) MarshalMultipartRequest(fields map[string]string, files map[string]string, log logger.Logger) ([]byte, string, error) {
6161
body := &bytes.Buffer{}
6262
writer := multipart.NewWriter(body)
6363

6464
// Add the simple fields to the form data
6565
for field, value := range fields {
66-
log.Debug("Adding field to multipart request", zap.String("Field", field), zap.String("Value", value))
6766
if err := writer.WriteField(field, value); err != nil {
68-
return nil, "", "", err
67+
return nil, "", err
6968
}
7069
}
7170

72-
// Add the files to the form data
71+
// Add the files to the form data, using safeOpenFile to ensure secure file access
7372
for formField, filePath := range files {
7473
file, err := helpers.SafeOpenFile(filePath)
7574
if err != nil {
7675
log.Error("Failed to open file securely", zap.String("file", filePath), zap.Error(err))
77-
return nil, "", "", err
76+
return nil, "", err
7877
}
7978
defer file.Close()
8079

8180
part, err := writer.CreateFormFile(formField, filepath.Base(filePath))
8281
if err != nil {
83-
return nil, "", "", err
82+
return nil, "", err
8483
}
85-
log.Debug("Adding file to multipart request", zap.String("FormField", formField), zap.String("FilePath", filePath))
8684
if _, err := io.Copy(part, file); err != nil {
87-
return nil, "", "", err
85+
return nil, "", err
8886
}
8987
}
9088

9189
// Close the writer to finish writing the multipart message
9290
if err := writer.Close(); err != nil {
93-
return nil, "", "", err
91+
return nil, "", err
9492
}
9593

9694
contentType := writer.FormDataContentType()
97-
bodyBytes := body.Bytes()
98-
99-
// Extract the first and last parts of the body for logging
100-
const logSegmentSize = 1024 // 1 KB
101-
bodyLen := len(bodyBytes)
102-
var logBody string
103-
if bodyLen <= 2*logSegmentSize {
104-
logBody = string(bodyBytes)
105-
} else {
106-
logBody = string(bodyBytes[:logSegmentSize]) + "..." + string(bodyBytes[bodyLen-logSegmentSize:])
107-
}
108-
109-
// Log the boundary and a partial body for debugging
110-
boundary := writer.Boundary()
111-
log.Debug("Multipart boundary", zap.String("Boundary", boundary))
112-
log.Debug("Multipart request body (partial)", zap.String("Body", logBody))
95+
log.Debug("Multipart request body", zap.String("Body", body.String())) // Log the body
11396

114-
return bodyBytes, contentType, logBody, nil
97+
return body.Bytes(), contentType, nil
11598
}

apiintegrations/msgraph/msgraph_api_request.go

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ func (g *GraphAPIHandler) MarshalRequest(body interface{}, method string, endpoi
3030
return data, nil
3131
}
3232

33-
// MarshalMultipartRequest encodes the request body as a multipart message for the Microsoft Graph API.
34-
func (g *GraphAPIHandler) MarshalMultipartRequest(fields map[string]string, files map[string]string, log logger.Logger) ([]byte, string, string, error) {
33+
// MarshalMultipartRequest handles multipart form data encoding with secure file handling and returns the encoded body and content type.
34+
func (g *GraphAPIHandler) MarshalMultipartRequest(fields map[string]string, files map[string]string, log logger.Logger) ([]byte, string, error) {
35+
3536
body := &bytes.Buffer{}
3637
writer := multipart.NewWriter(body)
3738

3839
// Add the simple fields to the form data
3940
for field, value := range fields {
40-
log.Debug("Adding field to multipart request", zap.String("Field", field), zap.String("Value", value))
4141
if err := writer.WriteField(field, value); err != nil {
42-
return nil, "", "", err
42+
return nil, "", err
4343
}
4444
}
4545

@@ -48,42 +48,24 @@ func (g *GraphAPIHandler) MarshalMultipartRequest(fields map[string]string, file
4848
file, err := helpers.SafeOpenFile(filePath)
4949
if err != nil {
5050
log.Error("Failed to open file securely", zap.String("file", filePath), zap.Error(err))
51-
return nil, "", "", err
51+
return nil, "", err
5252
}
5353
defer file.Close()
5454

5555
part, err := writer.CreateFormFile(formField, filepath.Base(filePath))
5656
if err != nil {
57-
return nil, "", "", err
57+
return nil, "", err
5858
}
59-
log.Debug("Adding file to multipart request", zap.String("FormField", formField), zap.String("FilePath", filePath))
6059
if _, err := io.Copy(part, file); err != nil {
61-
return nil, "", "", err
60+
return nil, "", err
6261
}
6362
}
6463

6564
// Close the writer to finish writing the multipart message
66-
if err := writer.Close(); err != nil {
67-
return nil, "", "", err
68-
}
69-
7065
contentType := writer.FormDataContentType()
71-
bodyBytes := body.Bytes()
72-
73-
// Extract the first and last parts of the body for logging
74-
const logSegmentSize = 1024 // 1 KB
75-
bodyLen := len(bodyBytes)
76-
var logBody string
77-
if bodyLen <= 2*logSegmentSize {
78-
logBody = string(bodyBytes)
79-
} else {
80-
logBody = string(bodyBytes[:logSegmentSize]) + "..." + string(bodyBytes[bodyLen-logSegmentSize:])
66+
if err := writer.Close(); err != nil {
67+
return nil, "", err
8168
}
8269

83-
// Log the boundary and a partial body for debugging
84-
boundary := writer.Boundary()
85-
log.Debug("Multipart boundary", zap.String("Boundary", boundary))
86-
log.Debug("Multipart request body (partial)", zap.String("Body", logBody))
87-
88-
return bodyBytes, contentType, logBody, nil
70+
return body.Bytes(), contentType, nil
8971
}

httpclient/multipartrequest.go

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ package httpclient
33

44
import (
55
"bytes"
6-
"io"
7-
"mime/multipart"
86
"net/http"
9-
"path/filepath"
107

118
"github.com/deploymenttheory/go-api-http-client/authenticationhandler"
129
"github.com/deploymenttheory/go-api-http-client/headers"
13-
"github.com/deploymenttheory/go-api-http-client/helpers"
1410
"github.com/deploymenttheory/go-api-http-client/response"
1511
"go.uber.org/zap"
1612
)
@@ -58,70 +54,23 @@ func (c *Client) DoMultipartRequest(method, endpoint string, fields map[string]s
5854
return nil, err
5955
}
6056

61-
// Create a buffer to hold the multipart form data
62-
body := &bytes.Buffer{}
63-
writer := multipart.NewWriter(body)
64-
65-
// Add the simple fields to the form data
66-
for field, value := range fields {
67-
log.Debug("Adding field to multipart request", zap.String("Field", field), zap.String("Value", value))
68-
if err := writer.WriteField(field, value); err != nil {
69-
return nil, err
70-
}
71-
}
72-
73-
// Add the files to the form data
74-
for formField, filePath := range files {
75-
file, err := helpers.SafeOpenFile(filePath)
76-
if err != nil {
77-
log.Error("Failed to open file securely", zap.String("file", filePath), zap.Error(err))
78-
return nil, err
79-
}
80-
defer file.Close()
81-
82-
part, err := writer.CreateFormFile(formField, filepath.Base(filePath))
83-
if err != nil {
84-
return nil, err
85-
}
86-
log.Debug("Adding file to multipart request", zap.String("FormField", formField), zap.String("FilePath", filePath))
87-
if _, err := io.Copy(part, file); err != nil {
88-
return nil, err
89-
}
90-
}
91-
92-
// Close the writer to finish writing the multipart message
93-
if err := writer.Close(); err != nil {
57+
// Marshal the multipart form data
58+
requestData, contentType, err := c.APIHandler.MarshalMultipartRequest(fields, files, log)
59+
if err != nil {
9460
return nil, err
9561
}
9662

97-
contentType := writer.FormDataContentType()
98-
bodyBytes := body.Bytes()
99-
100-
// Extract the first and last parts of the body for logging
101-
const logSegmentSize = 1024 // 1 KB
102-
bodyLen := len(bodyBytes)
103-
var logBody string
104-
if bodyLen <= 2*logSegmentSize {
105-
logBody = string(bodyBytes)
106-
} else {
107-
logBody = string(bodyBytes[:logSegmentSize]) + "..." + string(bodyBytes[bodyLen-logSegmentSize:])
108-
}
109-
110-
// Log the boundary and a partial body for debugging
111-
boundary := writer.Boundary()
112-
log.Debug("Multipart boundary", zap.String("Boundary", boundary))
113-
log.Debug("Multipart request body (partial)", zap.String("Body", logBody))
114-
11563
// Construct URL using the ConstructAPIResourceEndpoint function
11664
url := c.APIHandler.ConstructAPIResourceEndpoint(endpoint, log)
11765

11866
// Create the request
119-
req, err := http.NewRequest(method, url, bytes.NewBuffer(bodyBytes))
67+
req, err := http.NewRequest(method, url, bytes.NewBuffer(requestData))
12068
if err != nil {
12169
return nil, err
12270
}
12371

12472
// Initialize HeaderManager
73+
//log.Debug("Setting Authorization header with token", zap.String("Token", c.Token))
12574
headerHandler := headers.NewHeaderHandler(req, c.Logger, c.APIHandler, c.AuthTokenHandler)
12675

12776
// Use HeaderManager to set headers

0 commit comments

Comments
 (0)