oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package mergegate
2
3import (
4 "path"
5 "strings"
6 "unicode"
7
8 "amadan.net/oficina/equipo/internal/wire"
9)
10
11// Sensitive classes (spec §3.3). The table is code: a project may add
12// globs, nobody can remove one.
13const (
14 ClassMigrations = "migrations"
15 ClassAuthSecrets = "auth_secrets"
16 ClassPayments = "payments"
17 ClassCIConfig = "ci_config"
18 ClassDependencies = "dependencies"
19 ClassRelease = "release_signing"
20 ClassInfra = "infrastructure"
21 ClassProject = "project"
22)
23
24type area struct {
25 class string
26 globs []string
27 tokens []string
28}
29
30var areas = []area{
31 {ClassMigrations, []string{"**/migrations/**", "**/migrate/**", "**/*.sql", "**/schema.*", "**/*.prisma"}, []string{"migration", "migrations", "schema"}},
32 {ClassAuthSecrets, []string{"**/.env*", "**/*.pem", "**/*.key", "**/*.p12", "**/*.p8", "**/*.keystore", "**/*.jks"}, []string{"auth", "authn", "authz", "oauth", "oidc", "saml", "jwt", "login", "logout", "signin", "signup", "session", "sessions", "password", "passwords", "passwd", "credential", "credentials", "secret", "secrets", "token", "tokens", "key", "keys", "keychain", "crypto", "cipher", "encrypt", "decrypt", "cert", "certs", "certificate", "tls", "ssl", "permission", "permissions", "acl", "rbac"}},
33 {ClassPayments, nil, []string{"payment", "payments", "billing", "stripe", "checkout", "invoice", "invoices", "subscription", "subscriptions", "purchase", "purchases", "storekit", "iap", "paywall", "refund", "refunds"}},
34 {ClassCIConfig, []string{"**/.amadan/**", "**/.github/**", "**/.gitlab-ci.yml", "**/.circleci/**", "**/.buildkite/**", "**/Makefile", "**/GNUmakefile", "**/makefile", "**/*.mk"}, nil},
35 {ClassDependencies, []string{"**/go.mod", "**/go.sum", "**/go.work*", "**/package.json", "**/package-lock.json", "**/npm-shrinkwrap.json", "**/yarn.lock", "**/pnpm-lock.yaml", "**/bun.lock*", "**/Cargo.toml", "**/Cargo.lock", "**/Gemfile*", "**/requirements*.txt", "**/pyproject.toml", "**/poetry.lock", "**/Pipfile*", "**/uv.lock", "**/Package.swift", "**/Package.resolved", "**/Podfile*", "**/Cartfile*", "**/*.gradle*", "**/gradle.lockfile", "**/composer.json", "**/composer.lock", "**/mix.exs", "**/mix.lock", "**/.gitmodules", "**/vendor/**"}, nil},
36 {ClassRelease, []string{"**/*.entitlements", "**/*.mobileprovision", "**/ExportOptions*.plist", "**/fastlane/**", "**/*.xcconfig", "**/.goreleaser*", "**/VERSION"}, []string{"release", "releases", "signing", "entitlements", "provisioning"}},
37 {ClassInfra, []string{"**/*.tf", "**/*.tfvars", "**/*.hcl", "**/Dockerfile*", "**/docker-compose*", "**/*.service", "**/fly.toml", "**/vercel.json", "**/netlify.toml", "**/render.yaml", "**/Procfile", "**/app.yaml", "**/carlos.toml"}, []string{"terraform", "ansible", "pulumi", "cloudformation", "infra", "infrastructure", "deploy", "k8s", "helm"}},
38}
39
40// Match is one sensitive path and the class it falls in.
41type Match struct {
42 Class string `json:"class"`
43 Path string `json:"path"`
44}
45
46// Sensitive lists the entries' paths (old and new) that fall in a
47// sensitive class or match one of the project's extra globs.
48func Sensitive(entries []wire.ChangeEntry, extra []string) []Match {
49 var out []Match
50 for _, e := range entries {
51 for _, p := range []string{e.Old, e.New} {
52 if p == "" {
53 continue
54 }
55 if class := classOf(p, extra); class != "" {
56 out = append(out, Match{Class: class, Path: p})
57 }
58 }
59 }
60 return out
61}
62
63func classOf(p string, extra []string) string {
64 lower := strings.ToLower(p)
65 toks := tokens(p)
66 for _, a := range areas {
67 for _, g := range a.globs {
68 if glob(strings.ToLower(g), lower) {
69 return a.class
70 }
71 }
72 for _, t := range toks {
73 for _, want := range a.tokens {
74 if t == want {
75 return a.class
76 }
77 }
78 }
79 }
80 for _, g := range extra {
81 g = strings.ToLower(g)
82 if !strings.HasPrefix(g, "**/") {
83 g = "**/" + g
84 }
85 if glob(g, lower) {
86 return ClassProject
87 }
88 }
89 return ""
90}
91
92// tokens splits a path into lower-case words: on "/", ".", "-", "_" and
93// where a lower-case letter or digit is followed by an upper-case one.
94func tokens(p string) []string {
95 var out []string
96 var cur []rune
97 flush := func() {
98 if len(cur) > 0 {
99 out = append(out, strings.ToLower(string(cur)))
100 cur = cur[:0]
101 }
102 }
103 rs := []rune(p)
104 for i, r := range rs {
105 switch {
106 case r == '/' || r == '.' || r == '-' || r == '_' || r == ' ':
107 flush()
108 continue
109 case i > 0 && unicode.IsUpper(r) && (unicode.IsLower(rs[i-1]) || unicode.IsDigit(rs[i-1])):
110 flush()
111 }
112 cur = append(cur, r)
113 }
114 flush()
115 return out
116}
117
118// glob matches a slash path against a pattern whose segments are
119// path.Match patterns, "**" standing for any number of segments.
120func glob(pattern, name string) bool {
121 return segments(strings.Split(pattern, "/"), strings.Split(name, "/"))
122}
123
124func segments(ps, ns []string) bool {
125 for len(ps) > 0 {
126 if ps[0] == "**" {
127 for i := 0; i <= len(ns); i++ {
128 if segments(ps[1:], ns[i:]) {
129 return true
130 }
131 }
132 return false
133 }
134 if len(ns) == 0 {
135 return false
136 }
137 if ok, err := path.Match(ps[0], ns[0]); err != nil || !ok {
138 return false
139 }
140 ps, ns = ps[1:], ns[1:]
141 }
142 return len(ns) == 0
143}
144