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.
Implement SourceUnitUnmarshaller for all sources (trufflesecurity#1416)
* Implement CommonSourceUnitUnmarshaller * Add SourceUnitUnmarshaller to all sources using All sources, with the exception of git, will use the CommonSourceUnit as they only contain a single type of unit to scan. * Fix method comments to adhere to Go's style guide
- Loading branch information
1 parent
0c643bd
commit f3152b6
Showing
10 changed files
with
94 additions
and
9 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
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,40 @@ | ||
package git | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/sources" | ||
) | ||
|
||
const ( | ||
UnitRepo string = "repo" | ||
UnitDir string = "dir" | ||
) | ||
|
||
// Ensure SourceUnit implements the interface at compile time. | ||
var _ sources.SourceUnit = SourceUnit{} | ||
|
||
// A git source unit can be two kinds of units: either a local directory path | ||
// or a remote repository. | ||
type SourceUnit struct { | ||
Kind string `json:"kind"` | ||
ID string `json:"id"` | ||
} | ||
|
||
// Implement sources.SourceUnit interface. | ||
func (u SourceUnit) SourceUnitID() string { | ||
return u.ID | ||
} | ||
|
||
// Helper function to unmarshal raw bytes into our SourceUnit struct. | ||
func UnmarshalUnit(data []byte) (sources.SourceUnit, error) { | ||
var unit SourceUnit | ||
if err := json.Unmarshal(data, &unit); err != nil { | ||
return nil, err | ||
} | ||
if unit.ID == "" || (unit.Kind != UnitRepo && unit.Kind != UnitDir) { | ||
return nil, fmt.Errorf("not a git.SourceUnit") | ||
} | ||
return unit, 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 |
---|---|---|
@@ -1,12 +1,37 @@ | ||
package sources | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// Ensure CommonSourceUnit implements SourceUnit at compile time. | ||
var _ SourceUnit = CommonSourceUnit{} | ||
|
||
// CommonSourceUnit is a common implementation of SourceUnit that Sources can | ||
// use instead of implementing their own types. | ||
type CommonSourceUnit struct { | ||
ID string `json:"source_unit_id"` | ||
} | ||
|
||
// Implement the SourceUnit interface. | ||
// SourceUnitID implements the SourceUnit interface. | ||
func (c CommonSourceUnit) SourceUnitID() string { | ||
return c.ID | ||
} | ||
|
||
// CommonSourceUnitUnmarshaller is an implementation of SourceUnitUnmarshaller | ||
// for the CommonSourceUnit. A source can embed this struct to gain the | ||
// functionality of converting []byte to a CommonSourceUnit. | ||
type CommonSourceUnitUnmarshaller struct{} | ||
|
||
// UnmarshalSourceUnit implements the SourceUnitUnmarshaller interface. | ||
func (c CommonSourceUnitUnmarshaller) UnmarshalSourceUnit(data []byte) (SourceUnit, error) { | ||
var unit CommonSourceUnit | ||
if err := json.Unmarshal(data, &unit); err != nil { | ||
return nil, err | ||
} | ||
if unit.ID == "" { | ||
return nil, fmt.Errorf("not a CommonSourceUnit") | ||
} | ||
return unit, nil | ||
} |