forked from trufflesecurity/trufflehog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically update trufflehog (trufflesecurity#121)
Co-authored-by: Dustin Decker <[email protected]>
- Loading branch information
1 parent
aea9374
commit a8f07c8
Showing
6 changed files
with
232 additions
and
2 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
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,112 @@ | ||
package updater | ||
|
||
import ( | ||
"archive/tar" | ||
"archive/zip" | ||
"bytes" | ||
"compress/gzip" | ||
"encoding/json" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"runtime" | ||
"strings" | ||
"time" | ||
|
||
"github.com/go-errors/errors" | ||
"github.com/jpillora/overseer/fetcher" | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/version" | ||
) | ||
|
||
func Fetcher(version string) fetcher.Interface { | ||
return &OSS{ | ||
CurrentVersion: version, | ||
} | ||
} | ||
|
||
type OSS struct { | ||
Interval time.Duration | ||
CurrentVersion string | ||
} | ||
|
||
// Init validates the provided config | ||
func (g *OSS) Init() error { | ||
//initiate OSS connection | ||
return nil | ||
} | ||
|
||
const url = "https://oss.trufflehog.org/updates" | ||
|
||
type FormData struct { | ||
OS string | ||
Arch string | ||
CurrentVersion string | ||
Timezone string | ||
Binary string | ||
} | ||
|
||
// Fetch binary from URL via OSS client | ||
func (g *OSS) Fetch() (io.Reader, error) { | ||
log.Debug("fetching trufflehog update") | ||
|
||
zone, _ := time.Now().Zone() | ||
data := &FormData{ | ||
OS: runtime.GOOS, | ||
Arch: runtime.GOARCH, | ||
CurrentVersion: version.BuildVersion, | ||
Timezone: zone, | ||
Binary: "trufflehog", | ||
} | ||
|
||
dataByte, err := json.Marshal(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
reader := bytes.NewReader(dataByte) | ||
resp, err := http.Post(url, "application/json", reader) | ||
if err != nil || resp == nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
newBinBytes, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
buffer := bytes.NewReader(newBinBytes) | ||
switch runtime.GOOS { | ||
case "windows": | ||
zipReader, err := zip.NewReader(buffer, int64(len(newBinBytes))) | ||
if err != nil { | ||
return nil, errors.Errorf("Failed to read zip archive: %s", err) | ||
} | ||
for _, f := range zipReader.File { | ||
if strings.HasPrefix(f.Name, "trufflehog") { | ||
return f.Open() | ||
} | ||
} | ||
default: | ||
gzipReader, err := gzip.NewReader(buffer) | ||
if err != nil { | ||
return nil, errors.Errorf("Failed to read gzip archive: %s", err) | ||
} | ||
defer gzipReader.Close() | ||
tarReader := tar.NewReader(gzipReader) | ||
for { | ||
header, err := tarReader.Next() | ||
if err == io.EOF { | ||
return nil, errors.New("unable to get update") | ||
} | ||
|
||
if header.Typeflag == tar.TypeReg { | ||
if strings.HasPrefix(header.Name, "trufflehog") { | ||
return tarReader, nil | ||
} | ||
} | ||
} | ||
} | ||
return nil, errors.New("unable to get update") | ||
} |
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,3 @@ | ||
package version | ||
|
||
var BuildVersion = "dev" |