|
| 1 | +package precommit |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "path" |
| 9 | + "sort" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/step-security/secure-repo/remediation/workflow/permissions" |
| 13 | + "gopkg.in/yaml.v3" |
| 14 | +) |
| 15 | + |
| 16 | +type UpdatePrecommitConfigResponse struct { |
| 17 | + OriginalInput string |
| 18 | + FinalOutput string |
| 19 | + IsChanged bool |
| 20 | + ConfigfileFetchError bool |
| 21 | +} |
| 22 | + |
| 23 | +type UpdatePrecommitConfigRequest struct { |
| 24 | + Content string |
| 25 | + Languages []string |
| 26 | +} |
| 27 | + |
| 28 | +type PrecommitConfig struct { |
| 29 | + Repos []Repo `yaml:"repos"` |
| 30 | +} |
| 31 | + |
| 32 | +type Repo struct { |
| 33 | + Repo string `yaml:"repo"` |
| 34 | + Rev string `yaml:"rev"` |
| 35 | + Hooks []Hook `yaml:"hooks"` |
| 36 | +} |
| 37 | + |
| 38 | +type Hook struct { |
| 39 | + Id string `yaml:"id"` |
| 40 | +} |
| 41 | + |
| 42 | +type FetchPrecommitConfig struct { |
| 43 | + Hooks Hooks `yaml:"hooks"` |
| 44 | +} |
| 45 | + |
| 46 | +// type LangHook struct { |
| 47 | +// Repo Repo `yaml:"hook"` |
| 48 | +// } |
| 49 | + |
| 50 | +type Hooks map[string][]Repo |
| 51 | + |
| 52 | +func getConfigFile() (string, error) { |
| 53 | + filePath := os.Getenv("PRECOMMIT_CONFIG") |
| 54 | + |
| 55 | + if filePath == "" { |
| 56 | + filePath = "./" |
| 57 | + } |
| 58 | + |
| 59 | + configFile, err := ioutil.ReadFile(path.Join(filePath, "precommit-config.yml")) |
| 60 | + if err != nil { |
| 61 | + return "", err |
| 62 | + } |
| 63 | + |
| 64 | + return string(configFile), nil |
| 65 | +} |
| 66 | + |
| 67 | +func GetHooks(languages []string, alreadyPresentHooks map[string]bool) ([]Repo, error) { |
| 68 | + configFile, err := getConfigFile() |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + var fetchPrecommitConfig FetchPrecommitConfig |
| 73 | + yaml.Unmarshal([]byte(configFile), &fetchPrecommitConfig) |
| 74 | + newHooks := make(map[string]Repo) |
| 75 | + for _, lang := range languages { |
| 76 | + if _, ok := alreadyPresentHooks[fetchPrecommitConfig.Hooks[lang][0].Hooks[0].Id]; !ok { |
| 77 | + if repo, ok := newHooks[fetchPrecommitConfig.Hooks[lang][0].Repo]; ok { |
| 78 | + repo.Hooks = append(repo.Hooks, fetchPrecommitConfig.Hooks[lang][0].Hooks...) |
| 79 | + newHooks[fetchPrecommitConfig.Hooks[lang][0].Repo] = repo |
| 80 | + } else { |
| 81 | + newHooks[fetchPrecommitConfig.Hooks[lang][0].Repo] = fetchPrecommitConfig.Hooks[lang][0] |
| 82 | + } |
| 83 | + alreadyPresentHooks[fetchPrecommitConfig.Hooks[lang][0].Hooks[0].Id] = true |
| 84 | + } |
| 85 | + } |
| 86 | + // Adding common hooks |
| 87 | + var repos []Repo |
| 88 | + for _, repo := range fetchPrecommitConfig.Hooks["common"] { |
| 89 | + tempRepo := repo |
| 90 | + tempRepo.Hooks = nil |
| 91 | + hookPresent := false |
| 92 | + for _, hook := range repo.Hooks { |
| 93 | + if _, ok := alreadyPresentHooks[hook.Id]; !ok { |
| 94 | + tempRepo.Hooks = append(tempRepo.Hooks, hook) |
| 95 | + hookPresent = true |
| 96 | + } |
| 97 | + } |
| 98 | + if hookPresent { |
| 99 | + repos = append(repos, tempRepo) |
| 100 | + } |
| 101 | + } |
| 102 | + for _, repo := range newHooks { |
| 103 | + repos = append(repos, repo) |
| 104 | + } |
| 105 | + sort.Slice(repos, func(i, j int) bool { |
| 106 | + return repos[i].Repo < repos[j].Repo |
| 107 | + }) |
| 108 | + return repos, nil |
| 109 | +} |
| 110 | + |
| 111 | +func UpdatePrecommitConfig(precommitConfig string) (*UpdatePrecommitConfigResponse, error) { |
| 112 | + var updatePrecommitConfigRequest UpdatePrecommitConfigRequest |
| 113 | + json.Unmarshal([]byte(precommitConfig), &updatePrecommitConfigRequest) |
| 114 | + inputConfigFile := []byte(updatePrecommitConfigRequest.Content) |
| 115 | + configMetadata := PrecommitConfig{} |
| 116 | + err := yaml.Unmarshal(inputConfigFile, &configMetadata) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + |
| 121 | + response := new(UpdatePrecommitConfigResponse) |
| 122 | + response.FinalOutput = updatePrecommitConfigRequest.Content |
| 123 | + response.OriginalInput = updatePrecommitConfigRequest.Content |
| 124 | + response.IsChanged = false |
| 125 | + |
| 126 | + if updatePrecommitConfigRequest.Content == "" { |
| 127 | + response.FinalOutput = "repos:" |
| 128 | + } |
| 129 | + |
| 130 | + alreadyPresentHooks := make(map[string]bool) |
| 131 | + for _, repos := range configMetadata.Repos { |
| 132 | + for _, hook := range repos.Hooks { |
| 133 | + alreadyPresentHooks[hook.Id] = true |
| 134 | + } |
| 135 | + } |
| 136 | + // Contains a list of hooks to be added and not present in the file |
| 137 | + Hooks, err := GetHooks(updatePrecommitConfigRequest.Languages, alreadyPresentHooks) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + |
| 142 | + for _, Update := range Hooks { |
| 143 | + repoAlreadyExist := false |
| 144 | + for _, update := range configMetadata.Repos { |
| 145 | + if update.Repo == Update.Repo { |
| 146 | + repoAlreadyExist = true |
| 147 | + } |
| 148 | + if repoAlreadyExist { |
| 149 | + break |
| 150 | + } |
| 151 | + } |
| 152 | + response.FinalOutput, err = addHook(Update, repoAlreadyExist, response.FinalOutput) |
| 153 | + if err != nil { |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + response.IsChanged = true |
| 157 | + } |
| 158 | + |
| 159 | + return response, nil |
| 160 | +} |
| 161 | + |
| 162 | +func addHook(Update Repo, repoAlreadyExist bool, inputYaml string) (string, error) { |
| 163 | + t := yaml.Node{} |
| 164 | + |
| 165 | + err := yaml.Unmarshal([]byte(inputYaml), &t) |
| 166 | + if err != nil { |
| 167 | + return "", fmt.Errorf("unable to parse yaml %v", err) |
| 168 | + } |
| 169 | + |
| 170 | + spaces := "" |
| 171 | + jobNode := permissions.IterateNode(&t, "hooks", "!!seq", 0) |
| 172 | + if jobNode == nil { |
| 173 | + spaces = " " |
| 174 | + } else { |
| 175 | + for i := 0; i < jobNode.Column-1; i++ { |
| 176 | + spaces += " " |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + if repoAlreadyExist { |
| 181 | + jobNode = permissions.IterateNode(&t, Update.Repo, "!!str", 0) |
| 182 | + if jobNode == nil { |
| 183 | + return "", fmt.Errorf("Repo Name %s not found in the input yaml", Update.Repo) |
| 184 | + } |
| 185 | + |
| 186 | + // TODO: Also update rev version for already exist repo |
| 187 | + inputLines := strings.Split(inputYaml, "\n") |
| 188 | + var output []string |
| 189 | + for i := 0; i < jobNode.Line+1; i++ { |
| 190 | + output = append(output, inputLines[i]) |
| 191 | + } |
| 192 | + |
| 193 | + for _, hook := range Update.Hooks { |
| 194 | + output = append(output, spaces+fmt.Sprintf("- id: %s", hook.Id)) |
| 195 | + } |
| 196 | + |
| 197 | + for i := jobNode.Line + 1; i < len(inputLines); i++ { |
| 198 | + output = append(output, inputLines[i]) |
| 199 | + } |
| 200 | + return strings.Join(output, "\n"), nil |
| 201 | + } else { |
| 202 | + inputLines := strings.Split(inputYaml, "\n") |
| 203 | + inputLines = append(inputLines, fmt.Sprintf("- repo: %s", Update.Repo)) |
| 204 | + inputLines = append(inputLines, fmt.Sprintf(" rev: %s", Update.Rev)) |
| 205 | + inputLines = append(inputLines, fmt.Sprintf(" hooks:")) |
| 206 | + |
| 207 | + for _, hook := range Update.Hooks { |
| 208 | + inputLines = append(inputLines, spaces+fmt.Sprintf("- id: %s", hook.Id)) |
| 209 | + } |
| 210 | + return strings.Join(inputLines, "\n"), nil |
| 211 | + } |
| 212 | +} |
0 commit comments