-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new subcommand scan-snippets
- Loading branch information
Showing
7 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"github.com/murphysecurity/murphysec/utils" | ||
"github.com/murphysecurity/murphysec/utils/must" | ||
"io" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func SubmitCodeHash(ctx context.Context, client *Client, path string) error { | ||
checkNotNull(client) | ||
var u = joinURL(client.baseUrl, "/platform3/v3/client/upload_feature").String() | ||
var fBody = func() (io.ReadCloser, error) { | ||
f, e := os.Open(path) | ||
if e != nil { | ||
return nil, e | ||
} | ||
return utils.NewBufferedReader(f), nil | ||
} | ||
firstTimeBody, e := fBody() | ||
if e != nil { | ||
return e | ||
} | ||
var req = must.A(http.NewRequestWithContext(ctx, http.MethodPost, u, firstTimeBody)) | ||
req.GetBody = fBody | ||
req.Header.Set("Content-Type", "application/json") | ||
return client.DoJson(req, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package codehash | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"github.com/murphysecurity/murphysec/api" | ||
"github.com/murphysecurity/murphysec/infra/logctx" | ||
"github.com/murphysecurity/murphysec/model" | ||
"github.com/murphysecurity/murphysec/utils/must" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
) | ||
|
||
func Scan(ctx context.Context) { | ||
var logger = logctx.Use(ctx) | ||
var base = filepath.Dir(must.A(os.Executable())) | ||
|
||
var st = model.UseScanTask(ctx) | ||
tempFile := must.A(os.CreateTemp("", "codehash-output-*.json")) | ||
var tempFilePath = tempFile.Name() | ||
_ = tempFile.Close() | ||
_ = os.Remove(tempFilePath) | ||
defer func() { _ = os.Remove(tempFilePath) }() | ||
var cmd *exec.Cmd | ||
var shPath string | ||
if runtime.GOOS == "windows" { | ||
shPath = filepath.Join(base, "bin\\cli.bat") | ||
cmd = exec.CommandContext(ctx, shPath, st.ProjectPath, tempFilePath, st.SubtaskId) | ||
} else { | ||
shPath = filepath.Join(base, "bin/cli") | ||
fi := must.A(os.Stat(shPath)) | ||
must.Must(os.Chmod(shPath, fi.Mode()|0111)) | ||
cmd = exec.CommandContext(ctx, "sh", filepath.Join(base, "bin/cli"), st.ProjectPath, tempFilePath, st.SubtaskId) | ||
} | ||
stderr := must.A(cmd.StderrPipe()) | ||
go func() { | ||
defer func() { _ = stderr.Close() }() | ||
b := bufio.NewScanner(stderr) | ||
for b.Scan() { | ||
logger.Sugar().Infof("codehash[e]: %s", b.Text()) | ||
} | ||
}() | ||
e := cmd.Run() | ||
if e != nil { | ||
logger.Sugar().Errorf("codehash error: %s", e.Error()) | ||
return | ||
} | ||
e = api.SubmitCodeHash(ctx, api.DefaultClient(), tempFilePath) | ||
if e != nil { | ||
logger.Sugar().Errorf("codehash submit error: %s", e.Error()) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters