-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add bsf dockerfile digests command
Signed-off-by: hanshal101 <82961842+hanshal101@users.noreply.github.com>
- Loading branch information
1 parent
26b2946
commit 5a0a92a
Showing
6 changed files
with
108 additions
and
98 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,3 @@ | ||
FROM ubuntu@sha256:0b897358ff6624825fb50d20ffb605ab0eaea77ced0adb8c6a4b756513dec6fc | ||
FROM python@sha256:a6c12ec09f13df9d4b8b4e4d08678c1b212d89885be14b6c72b633bee2a520f4 | ||
FROM node@sha256:a158d3b9b4e3fa813fa6c8c590b8f0a860e015ad4e59bbce5744d2f6fd8461aa14 |
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 was deleted.
Oops, something went wrong.
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,72 @@ | ||
package dockerfile | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/buildsafedev/bsf/cmd/styles" | ||
"github.com/google/go-containerregistry/pkg/crane" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var DGCmd = &cobra.Command{ | ||
Use: "dockerfile digests", | ||
Short: "Replace Dockerfile image tags with immutable digests", | ||
Aliases: []string{"dg"}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) < 1 { | ||
fmt.Println(styles.HintStyle.Render("hint:", "run `bsf dockerfile digests <Dockerfile>` to replace image tags with digests")) | ||
os.Exit(1) | ||
} | ||
|
||
dockerfile := args[1] | ||
file, err := os.Open(dockerfile) | ||
if err != nil { | ||
fmt.Printf("Error opening Dockerfile: %v\n", err) | ||
os.Exit(1) | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
re := regexp.MustCompile(`FROM\s+(\S+):(\S+)`) | ||
var updatedLines []string | ||
|
||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if matches := re.FindStringSubmatch(line); matches != nil { | ||
image := matches[1] | ||
tag := matches[2] | ||
digest, err := getDigest(image, tag) | ||
if err != nil { | ||
fmt.Printf("Error retrieving digest for %s:%s: %v\n", image, tag, err) | ||
os.Exit(1) | ||
} | ||
line = strings.Replace(line, fmt.Sprintf("%s:%s", image, tag), fmt.Sprintf("%s@%s", image, digest), 1) | ||
} | ||
updatedLines = append(updatedLines, line) | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
fmt.Printf("Error reading Dockerfile: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
if err := os.WriteFile(dockerfile, []byte(strings.Join(updatedLines, "\n")), 0644); err != nil { | ||
fmt.Printf("Error writing updated Dockerfile: %v\n", err) | ||
os.Exit(1) | ||
} | ||
fmt.Println("Dockerfile updated with image digests successfully.") | ||
}, | ||
} | ||
|
||
func getDigest(image, tag string) (string, error) { | ||
digest, err := crane.Digest(fmt.Sprintf("%s:%s", image, tag)) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get manifest for %s:%s: %w", image, tag, err) | ||
} | ||
|
||
return digest, 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