oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package isolatedjob
2
3import (
4 "fmt"
5 "strings"
6)
7
8// maxAttemptIDLen bounds an attempt ID. The ID becomes the provider's client
9// token (AWS takes 1 to 128 characters), so 64 leaves room.
10const maxAttemptIDLen = 64
11
12// Validate checks s against the provider limits l before any provider call.
13//
14// - AttemptID: 1 to 64 characters, each an ASCII letter, digit or '-'.
15// - Image: "sha256:" and 64 lower-case hex digits.
16// - Size: SizeSmall or SizeLarge.
17// - MaxLifetime: above zero and no more than the smaller of l.MaxLifetime
18// and HardMaxLifetime.
19// - Bootstrap: 1 to l.MaxBootstrapBytes bytes.
20//
21// Every failure wraps ErrInvalid, except an oversized bootstrap, which wraps
22// ErrBootstrapTooLarge.
23func (s LaunchSpec) Validate(l Limits) error {
24 if !validAttemptID(s.AttemptID) {
25 return fmt.Errorf("%w: attempt id must be 1 to %d letters, digits or '-'", ErrInvalid, maxAttemptIDLen)
26 }
27 if !validDigest(s.Image) {
28 return fmt.Errorf("%w: image must be sha256: and 64 lower-case hex digits", ErrInvalid)
29 }
30 if s.Size.MemoryMiB() == 0 {
31 return fmt.Errorf("%w: size %q is not %q or %q", ErrInvalid, s.Size, SizeSmall, SizeLarge)
32 }
33 limit := min(l.MaxLifetime, HardMaxLifetime)
34 if s.MaxLifetime <= 0 || s.MaxLifetime > limit {
35 return fmt.Errorf("%w: lifetime %v must be above zero and at most %v", ErrInvalid, s.MaxLifetime, limit)
36 }
37 if len(s.Bootstrap) == 0 {
38 return fmt.Errorf("%w: bootstrap is empty", ErrInvalid)
39 }
40 if len(s.Bootstrap) > l.MaxBootstrapBytes {
41 return fmt.Errorf("%w: bootstrap is %d bytes, limit %d", ErrBootstrapTooLarge, len(s.Bootstrap), l.MaxBootstrapBytes)
42 }
43 return nil
44}
45
46func validAttemptID(id string) bool {
47 if len(id) == 0 || len(id) > maxAttemptIDLen {
48 return false
49 }
50 for i := 0; i < len(id); i++ {
51 c := id[i]
52 if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-') {
53 return false
54 }
55 }
56 return true
57}
58
59func validDigest(d ImageDigest) bool {
60 hex, ok := strings.CutPrefix(string(d), "sha256:")
61 if !ok || len(hex) != 64 {
62 return false
63 }
64 for i := 0; i < len(hex); i++ {
65 c := hex[i]
66 if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f') {
67 return false
68 }
69 }
70 return true
71}
72