-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mpx 24 coap #1
base: main
Are you sure you want to change the base?
Mpx 24 coap #1
Changes from all commits
3ca34d6
7f20209
c8011e6
79bc3c5
247100b
46c672c
78a0d6f
6a9c7e3
7f0cbb2
0f62fe0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,13 +8,15 @@ import ( | |||||
|
||||||
mptls "github.com/absmach/mproxy/pkg/tls" | ||||||
"github.com/caarlos0/env/v11" | ||||||
"github.com/pion/dtls/v2" | ||||||
) | ||||||
|
||||||
type Config struct { | ||||||
Address string `env:"ADDRESS" envDefault:""` | ||||||
PathPrefix string `env:"PATH_PREFIX" envDefault:"/"` | ||||||
Target string `env:"TARGET" envDefault:""` | ||||||
TLSConfig *tls.Config | ||||||
DTLSConfig *dtls.Config | ||||||
} | ||||||
|
||||||
func NewConfig(opts env.Options) (Config, error) { | ||||||
|
@@ -27,8 +29,11 @@ func NewConfig(opts env.Options) (Config, error) { | |||||
if err != nil { | ||||||
return Config{}, err | ||||||
} | ||||||
|
||||||
c.TLSConfig, err = mptls.Load(&cfg) | ||||||
c.TLSConfig, err = mptls.LoadTLSConfig(&cfg, &tls.Config{}) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set the minimum TLS version to TLS 1.3 to enhance security. - c.TLSConfig, err = mptls.LoadTLSConfig(&cfg, &tls.Config{})
+ c.TLSConfig, err = mptls.LoadTLSConfig(&cfg, &tls.Config{MinVersion: tls.VersionTLS13}) Committable suggestion
Suggested change
|
||||||
if err != nil { | ||||||
return Config{}, err | ||||||
} | ||||||
c.DTLSConfig, err = mptls.LoadTLSConfig(&cfg, &dtls.Config{}) | ||||||
if err != nil { | ||||||
return Config{}, err | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,25 @@ | ||||||
#!/bin/bash | ||||||
protocol=coaps | ||||||
host=localhost | ||||||
port=5684 | ||||||
path="test" | ||||||
content=0x32 | ||||||
message="{\"message\": \"Hello mProxy\"}" | ||||||
auth="TOKEN" | ||||||
cafile=ssl/certs/ca.crt | ||||||
certfile=ssl/certs/client.crt | ||||||
keyfile=ssl/certs/client.key | ||||||
Comment on lines
+1
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure secure handling of sensitive data. The |
||||||
|
||||||
echo "Posting message to ${protocol}://${host}:${port}/${path} with dtls ..." | ||||||
coap-client -m post coap://${host}:${port}/${path} -e "${message}" -O 12,${content} -O 15,auth=${auth} \ | ||||||
-c $certfile -k $keyfile -C $cafile | ||||||
|
||||||
echo "Getting message from ${protocol}://${host}:${port}/${path} with dtls ..." | ||||||
coap-client -m get coap://${host}:${port}/${path} -O 6,0x00 -O 15,auth=${auth} -c $certfile -k $keyfile -C $cafile | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure the command is split correctly over multiple lines. There should be a space before the backslash to ensure the command is split correctly. - coap-client -m get coap://${host}:${port}/${path} -O 6,0x00 -O 15,auth=${auth} -c $certfile -k $keyfile -C $cafile
+ coap-client -m get coap://${host}:${port}/${path} -O 6,0x00 -O 15,auth=${auth} -c $certfile -k $keyfile -C $cafile \ Committable suggestion
Suggested change
|
||||||
|
||||||
echo "Posting message to ${protocol}://${host}:${port}/${path} with dtls and invalid client certificate..." | ||||||
coap-client -m post coap://${host}:${port}/${path} -e "${message}" -O 12,${content} -O 15,auth=${auth} \ | ||||||
-c ssl/certs/client_unknown.crt -k ssl/certs/client_unknown.key -C $cafile | ||||||
|
||||||
echo "Getting message from ${protocol}://${host}:${port}/${path} with dtls and invalid client certificate..." | ||||||
coap-client -m get coap://${host}:${port}/${path} -O 6,0x00 -O 15,auth=${auth} -c ssl/certs/client_unknown.crt -k ssl/certs/client_unknown.key -C $cafile |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/bash | ||
protocol=coap | ||
host=localhost | ||
port=5682 | ||
path="test" | ||
content=0x32 | ||
message="{\"message\": \"Hello mProxy\"}" | ||
auth="TOKEN" | ||
|
||
#Examples using lib-coap coap-client | ||
echo "Posting message to ${protocol}://${host}:${port}/${path} without tls ..." | ||
coap-client -m post coap://${host}:${port}/${path} -e "${message}" -O 12,${content} -O 15,auth=${auth} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification Ensure proper error handling for the
Consider adding error handling like this: coap-client -m post coap://${host}:${port}/${path} -e "${message}" -O 12,${content} -O 15,auth=${auth} || { echo "Failed to post message"; exit 1; } Analysis chainEnsure proper error handling for the Scripts ExecutedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check if error handling is implemented for coap-client commands.
# Test: Search for error handling patterns in coap-client usage.
rg --type sh $'coap-client.*|| echo'
Length of output: 18925 |
||
|
||
echo "Getting message from ${protocol}://${host}:${port}/${path} without tls ..." | ||
coap-client -m get coap://${host}:${port}/${path} -O 6,0x00 -O 15,auth=${auth} | ||
|
||
#Examples using Magisrala coap-cli | ||
echo "Posting message to ${protocol}://${host}:${port}/${path} without tls ..." | ||
coap-cli post ${host}:${port}/${path} -d "${message}" -O 12,${content} -O 15,auth=${auth} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification The script
Analysis chainConsider adding error handling for Scripts ExecutedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify error handling for coap-cli commands.
# Test: Search for error handling patterns in coap-cli usage.
rg --type sh $'coap-cli.*|| echo'
Length of output: 18922 |
||
|
||
echo "Getting message from ${protocol}://${host}:${port}/${path} without tls ..." | ||
coap-cli get ${host}:${port}/${path} -O 6,0x00 -O 15,auth=${auth} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) Abstract Machines | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
coap "github.com/plgd-dev/go-coap/v3" | ||
"github.com/plgd-dev/go-coap/v3/message" | ||
"github.com/plgd-dev/go-coap/v3/message/codes" | ||
"github.com/plgd-dev/go-coap/v3/mux" | ||
) | ||
|
||
const defaultPort = "5683" | ||
|
||
func handleRequest(w mux.ResponseWriter, r *mux.Message) { | ||
resp := w.Conn().AcquireMessage(r.Context()) | ||
defer w.Conn().ReleaseMessage(resp) | ||
resp.SetCode(codes.Content) | ||
resp.SetToken(r.Token()) | ||
resp.SetContentFormat(message.TextPlain) | ||
resp.SetBody(strings.NewReader(fmt.Sprintf("%v OK", r.Code()))) | ||
err := w.Conn().WriteMessage(resp) | ||
if err != nil { | ||
log.Printf("Cannot send response: %v", err) | ||
} | ||
} | ||
|
||
func main() { | ||
r := mux.NewRouter() | ||
r.DefaultHandle(mux.HandlerFunc(handleRequest)) | ||
log.Println("starting coap server, listening on port " + defaultPort) | ||
log.Fatal(coap.ListenAndServe("udp", ":"+defaultPort, r)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tip
Codebase Verification
The newly imported
coap
package (github.com/absmach/mproxy/pkg/coap
) is not utilized anywhere in the codebase. Please ensure that this import is either used appropriately in the code or removed if it is not needed.cmd/main.go
at line 16.Analysis chain
Ensure the new import
github.com/absmach/mproxy/pkg/coap
is utilized properly in the code.Scripts Executed
The following scripts were executed for the analysis:
Script:
Length of output: 558
Script:
Length of output: 50