logsafe 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +24 -0
- data/CODE_OF_CONDUCT.md +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +654 -0
- data/exe/logsafe +50 -0
- data/lib/logsafe/cipher.rb +170 -0
- data/lib/logsafe/copies.rb +176 -0
- data/lib/logsafe/day.rb +139 -0
- data/lib/logsafe/merge_job.rb +13 -0
- data/lib/logsafe/private_folder.rb +29 -0
- data/lib/logsafe/reencrypt.rb +107 -0
- data/lib/logsafe/reencrypt_job.rb +12 -0
- data/lib/logsafe/room.rb +35 -0
- data/lib/logsafe/search.rb +121 -0
- data/lib/logsafe/source.rb +79 -0
- data/lib/logsafe/storage.rb +335 -0
- data/lib/logsafe/version.rb +5 -0
- data/lib/logsafe/web.html.erb +105 -0
- data/lib/logsafe/web.rb +150 -0
- data/lib/logsafe.rb +159 -0
- data/shipper/start +18 -0
- data/shipper/to-folder.yaml +20 -0
- data/shipper/to-gcs.yaml +21 -0
- data/shipper/to-s3.yaml +23 -0
- data/shipper/vector.yaml +113 -0
- metadata +90 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<meta name="robots" content="noindex">
|
|
7
|
+
<title>Logs</title>
|
|
8
|
+
<style>
|
|
9
|
+
:root {
|
|
10
|
+
color-scheme: dark;
|
|
11
|
+
--ink: #17212b; --raised: #1e2a36; --bar: #223040; --edge: #3d5066; --well: #111a23;
|
|
12
|
+
--text: #d5dce4; --dim: #8494a6; --link: #8ec5ff; --amber: #f2c744; --red: #e5534b;
|
|
13
|
+
--mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
|
|
14
|
+
}
|
|
15
|
+
* { box-sizing: border-box; }
|
|
16
|
+
html, body { height: 100%; margin: 0; }
|
|
17
|
+
body { display: flex; flex-direction: column; background: var(--ink); color: var(--text); font: 13px/1.55 var(--mono); }
|
|
18
|
+
a { color: var(--link); }
|
|
19
|
+
:focus-visible { outline: 2px solid var(--link); outline-offset: 1px; }
|
|
20
|
+
|
|
21
|
+
/* Upside down, so the page opens at its end: the newest line, just above the search. */
|
|
22
|
+
main { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column-reverse; }
|
|
23
|
+
ol { list-style: none; margin: 0; padding: 8px 0; }
|
|
24
|
+
li { padding: 1px 16px 1px 13px; border-left: 3px solid transparent; white-space: pre-wrap; overflow-wrap: anywhere; }
|
|
25
|
+
li:hover { background: var(--raised); }
|
|
26
|
+
li.error { border-left-color: var(--red); color: #ffb4aa; }
|
|
27
|
+
li.warn { border-left-color: var(--amber); color: #f0d48a; }
|
|
28
|
+
time { color: var(--dim); }
|
|
29
|
+
mark { background: var(--amber); color: var(--ink); border-radius: 2px; padding: 0 1px; }
|
|
30
|
+
.turn { display: block; padding: 10px 16px; }
|
|
31
|
+
.note { margin: 0; padding: 24px 16px; max-width: 72ch; color: var(--dim); font: 14px/1.5 system-ui, sans-serif; }
|
|
32
|
+
.note + .note { padding-top: 0; }
|
|
33
|
+
.note.problem { color: #ffb4aa; }
|
|
34
|
+
|
|
35
|
+
form { display: flex; flex-wrap: wrap; align-items: end; gap: 8px 10px; padding: 10px 16px 12px;
|
|
36
|
+
background: var(--bar); border-top: 1px solid var(--edge); font: 12px system-ui, sans-serif; }
|
|
37
|
+
label { display: grid; gap: 3px; color: #a9b6c5; }
|
|
38
|
+
label.words { flex: 1 1 260px; }
|
|
39
|
+
input, select, button { height: 34px; padding: 0 10px; border: 1px solid var(--edge); border-radius: 4px;
|
|
40
|
+
background: var(--well); color: var(--text); font: 14px system-ui, sans-serif; }
|
|
41
|
+
input[type=search] { width: 100%; font-family: var(--mono); }
|
|
42
|
+
button { border-color: var(--amber); background: var(--amber); color: var(--ink); font-weight: 600; cursor: pointer; }
|
|
43
|
+
</style>
|
|
44
|
+
</head>
|
|
45
|
+
<body>
|
|
46
|
+
<main>
|
|
47
|
+
<div>
|
|
48
|
+
<%- @problems.each do |problem| -%>
|
|
49
|
+
<p class="note problem"><%= h(problem) %></p>
|
|
50
|
+
<%- end -%>
|
|
51
|
+
<%- if @cut -%>
|
|
52
|
+
<p class="note">A search spans <%= MAX_DAYS %> days at most, so this one starts on <%= @from %>.</p>
|
|
53
|
+
<%- end -%>
|
|
54
|
+
<%- if @waiting -%>
|
|
55
|
+
<p class="note">This search was opened from another site. Press Search to run it.</p>
|
|
56
|
+
<%- elsif @lines.empty? && @problems.empty? -%>
|
|
57
|
+
<p class="note"><%= @streams.empty? ? "The storage has no logs for these days." : "No lines match. Try fewer words, another level or other days." %></p>
|
|
58
|
+
<%- end -%>
|
|
59
|
+
<%- if @crowded -%>
|
|
60
|
+
<p class="note">More than <%= Search::SAME %> lines have the time <%= h(@crowded) %>. To see the rest of them, search for a word they hold.</p>
|
|
61
|
+
<%- end -%>
|
|
62
|
+
<%- if @older -%>
|
|
63
|
+
<a class="turn" href="<%= h(@older) %>">Show older lines</a>
|
|
64
|
+
<%- end -%>
|
|
65
|
+
<ol>
|
|
66
|
+
<%- @lines.each do |stream, line| -%>
|
|
67
|
+
<%- kind, time, text = row(line) -%>
|
|
68
|
+
<li<%= %( class="#{kind}") if kind %>><time datetime="<%= h(time) %>"><%= h(time.to_s[0, 23].tr("T", " ")) %></time> <b style="color: hsl(<%= hue(stream) %> 65% 72%)"><%= h(stream) %></b> <%= marked(text) %></li>
|
|
69
|
+
<%- end -%>
|
|
70
|
+
</ol>
|
|
71
|
+
<%- if @before -%>
|
|
72
|
+
<a class="turn" href="<%= h(link) %>">Back to the newest lines</a>
|
|
73
|
+
<%- end -%>
|
|
74
|
+
</div>
|
|
75
|
+
</main>
|
|
76
|
+
<form method="get">
|
|
77
|
+
<label class="words">Words, all must be in the line
|
|
78
|
+
<input type="search" name="q" value="<%= h(@query) %>" autofocus autocomplete="off" spellcheck="false">
|
|
79
|
+
</label>
|
|
80
|
+
<label>Stream
|
|
81
|
+
<select name="stream">
|
|
82
|
+
<option value="">All streams</option>
|
|
83
|
+
<%- (@streams | [*@stream]).each do |stream| -%>
|
|
84
|
+
<option<%= " selected" if stream == @stream %>><%= h(stream) %></option>
|
|
85
|
+
<%- end -%>
|
|
86
|
+
</select>
|
|
87
|
+
</label>
|
|
88
|
+
<label>Level
|
|
89
|
+
<select name="level">
|
|
90
|
+
<option value="">Any level</option>
|
|
91
|
+
<%- Logsafe::Search::LEVELS.each_key do |level| -%>
|
|
92
|
+
<option<%= " selected" if level == @level %>><%= level %></option>
|
|
93
|
+
<%- end -%>
|
|
94
|
+
</select>
|
|
95
|
+
</label>
|
|
96
|
+
<label>From (UTC)
|
|
97
|
+
<input type="date" name="from" value="<%= @from %>" required>
|
|
98
|
+
</label>
|
|
99
|
+
<label>To (UTC)
|
|
100
|
+
<input type="date" name="to" value="<%= @to %>" required>
|
|
101
|
+
</label>
|
|
102
|
+
<button>Search</button>
|
|
103
|
+
</form>
|
|
104
|
+
</body>
|
|
105
|
+
</html>
|
data/lib/logsafe/web.rb
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "date"
|
|
4
|
+
require "erb"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Logsafe
|
|
8
|
+
# The page to read and search the logs on. A Rack app, to mount where you like:
|
|
9
|
+
# mount Logsafe::Web, at: "logsafe"
|
|
10
|
+
# It does not ask who is looking: put it behind your own sign-in, see the README.
|
|
11
|
+
class Web
|
|
12
|
+
include ERB::Util
|
|
13
|
+
|
|
14
|
+
PAGE = ERB.new(File.read(File.join(__dir__, "web.html.erb")), trim_mode: "-")
|
|
15
|
+
MAX_DAYS = 7 # each day searched is fetched, and kept on this machine for a while
|
|
16
|
+
TIME = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}Z\z/
|
|
17
|
+
LOUD = /\b(?:(fatal|error)|warn(?:ing)?)\b/i
|
|
18
|
+
QUERY = 500 # characters that can be searched for
|
|
19
|
+
HEADERS = {
|
|
20
|
+
"content-type" => "text/html; charset=utf-8",
|
|
21
|
+
"cache-control" => "no-store",
|
|
22
|
+
"x-frame-options" => "DENY",
|
|
23
|
+
"cross-origin-opener-policy" => "same-origin",
|
|
24
|
+
"content-security-policy" => "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'"
|
|
25
|
+
}.freeze
|
|
26
|
+
# Each response gets a copy of its own: what stands in front of this app, such as Rack::ETag, adds to them.
|
|
27
|
+
TEXT = { "content-type" => "text/plain" }.freeze
|
|
28
|
+
|
|
29
|
+
def self.call(env)
|
|
30
|
+
return [405, TEXT.merge("allow" => "GET, HEAD"), ["Only GET"]] unless %w[GET HEAD].include?(env["REQUEST_METHOD"])
|
|
31
|
+
|
|
32
|
+
params = begin
|
|
33
|
+
URI.decode_www_form(env["QUERY_STRING"].to_s).to_h.transform_values { |value| value.scrub.delete("\0") }
|
|
34
|
+
rescue ArgumentError
|
|
35
|
+
return [400, TEXT.dup, ["Bad request"]]
|
|
36
|
+
end
|
|
37
|
+
new(params).response(search: here?(env))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Whether the page was opened from itself or by hand. A search fetches and decrypts days of logs, and
|
|
41
|
+
# takes a time that depends on what is in them: another website must not be able to start one with
|
|
42
|
+
# the session of someone who is signed in. Browsers that do not say are let through.
|
|
43
|
+
def self.here?(env)
|
|
44
|
+
["same-origin", "none", nil].include?(env["HTTP_SEC_FETCH_SITE"])
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Nothing from the address is used before it is checked: these values end up in paths and commands.
|
|
48
|
+
def initialize(params, today: Time.now.utc.to_date)
|
|
49
|
+
@to = date(params["to"]) || today
|
|
50
|
+
@from, @cut = from(date(params["from"]) || today)
|
|
51
|
+
look_for(params["q"].to_s, params["stream"].to_s, params["level"])
|
|
52
|
+
turn(params["before"].to_s[TIME], params["skip"])
|
|
53
|
+
@streams = []
|
|
54
|
+
@lines = []
|
|
55
|
+
@problems = []
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# What Logsafe itself refuses, such as a missing key, is said on the page. The rest is the app's to handle.
|
|
59
|
+
def response(search: true)
|
|
60
|
+
search ? find : @waiting = true
|
|
61
|
+
page(200)
|
|
62
|
+
rescue Error => e
|
|
63
|
+
@problems = [e.message]
|
|
64
|
+
page(500)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
def look_for(words, stream, level)
|
|
70
|
+
@query = words.strip[0, QUERY]
|
|
71
|
+
@words = @query.split.uniq.first(Search::WORDS)
|
|
72
|
+
@stream = stream[/.+/m] # none when it is empty
|
|
73
|
+
@level = level if Search::LEVELS.key?(level)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# To the lines before a time, and how many of that time itself to skip: see Search.
|
|
77
|
+
def turn(before, skip)
|
|
78
|
+
@before = before
|
|
79
|
+
@skip = skip.to_i.clamp(0, Search::SAME) if before && skip
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def page(status)
|
|
83
|
+
@older = older
|
|
84
|
+
[status, HEADERS.dup, [PAGE.result(binding)]]
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The stream asked for is only compared with the ones there are: a name that is none finds nothing.
|
|
88
|
+
def find
|
|
89
|
+
days = Search.sources(@from, @to)
|
|
90
|
+
@streams = days.keys.map(&:first).uniq.sort
|
|
91
|
+
days = days.select { |(stream, _), _| stream == @stream } if @stream
|
|
92
|
+
if !@stream && @streams.size > Search::STREAMS
|
|
93
|
+
@problems = ["There are #{@streams.size} streams in these days. Choose one."]
|
|
94
|
+
else
|
|
95
|
+
@lines, @problems = Search.call(days, words: @words, level: @level, before: @before, skip: @skip)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# The first day to search, and whether that is later than was asked for.
|
|
100
|
+
def from(asked)
|
|
101
|
+
from = asked.clamp(@to - (MAX_DAYS - 1), @to)
|
|
102
|
+
[from, asked < from]
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def date(text)
|
|
106
|
+
Date.strptime(text, "%Y-%m-%d") if text.to_s.match?(DATE)
|
|
107
|
+
rescue Date::Error
|
|
108
|
+
nil
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# The address of this search, with something changed.
|
|
112
|
+
def link(**changes)
|
|
113
|
+
search = { q: @query, stream: @stream, level: @level, from: @from, to: @to }
|
|
114
|
+
"?#{URI.encode_www_form(search.merge(changes).compact)}"
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# A page goes on from the time of its first line, and from how many lines of that time were shown:
|
|
118
|
+
# the rest of them belong on the next page.
|
|
119
|
+
def older
|
|
120
|
+
time = @lines.first.last[/\A\S+/].to_s[TIME] if @lines.size == Search::LIMIT
|
|
121
|
+
return unless time
|
|
122
|
+
|
|
123
|
+
shown = @lines.count { |_, line| line.start_with?(time) }
|
|
124
|
+
shown += @skip.to_i if time == @before
|
|
125
|
+
@crowded = time if shown > Search::SAME
|
|
126
|
+
link(before: time, skip: (shown unless @crowded))
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# A line as [class of the row, time, text].
|
|
130
|
+
def row(line)
|
|
131
|
+
time, text = line.split(" ", 2)
|
|
132
|
+
found = text.to_s.match(LOUD)
|
|
133
|
+
[found && (found[1] ? "error" : "warn"), time, text.to_s]
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# The text with the words that were searched for marked. Split first and escaped after, piece by piece.
|
|
137
|
+
def marked(text)
|
|
138
|
+
return h(text) if @words.empty?
|
|
139
|
+
|
|
140
|
+
@marks ||= /(#{Regexp.union(@words).source})/i
|
|
141
|
+
text.split(@marks).each_slice(2).map do |plain, word|
|
|
142
|
+
"#{h(plain)}#{"<mark>#{h(word)}</mark>" if word}"
|
|
143
|
+
end.join
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def hue(stream)
|
|
147
|
+
90 + (stream.sum * 137 % 240) # green to purple: red and amber mean something on this page
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
data/lib/logsafe.rb
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "date"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
require "pathname"
|
|
6
|
+
require "tmpdir"
|
|
7
|
+
require_relative "logsafe/version"
|
|
8
|
+
|
|
9
|
+
# Turns the parts a log shipper left in storage into one sorted file per stream per day:
|
|
10
|
+
# parts/<stream>/<date>/* -> <year>/<month>/<stream>.<date>.log.gz
|
|
11
|
+
# With a key, encrypted: see Logsafe::Cipher.
|
|
12
|
+
module Logsafe
|
|
13
|
+
class Error < StandardError; end
|
|
14
|
+
|
|
15
|
+
autoload :MergeJob, "logsafe/merge_job"
|
|
16
|
+
autoload :Reencrypt, "logsafe/reencrypt"
|
|
17
|
+
autoload :ReencryptJob, "logsafe/reencrypt_job"
|
|
18
|
+
autoload :Copies, "logsafe/copies"
|
|
19
|
+
autoload :Search, "logsafe/search"
|
|
20
|
+
autoload :Web, "logsafe/web"
|
|
21
|
+
|
|
22
|
+
PARTS = "parts/"
|
|
23
|
+
DATE = /\A\d{4}-\d{2}-\d{2}\z/
|
|
24
|
+
GZIP = "\x1F\x8B".b
|
|
25
|
+
# What a stream can be called, as in shipper/vector.yaml: it becomes part of names of files. And what
|
|
26
|
+
# is a part: nothing that could forge a line where its name is printed.
|
|
27
|
+
STREAM = /[A-Za-z0-9][A-Za-z0-9._-]{0,199}/
|
|
28
|
+
PART = %r{\A#{PARTS}(#{STREAM})/(\d{4}-\d{2}-\d{2})/[^[:cntrl:]]+\z}
|
|
29
|
+
AGE = 366 # days. A part of a date before that is left alone: there are too many such dates to make up.
|
|
30
|
+
RUN = 50 * 60 # seconds after which a merge starts with no other day, so that hourly runs do not pile up
|
|
31
|
+
|
|
32
|
+
class << self
|
|
33
|
+
# Where the logs are: a folder, { s3: "bucket", ... } or { gcs: "bucket", ... }. See the README.
|
|
34
|
+
def storage=(setting)
|
|
35
|
+
@storage = Storage.build(setting)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def storage
|
|
39
|
+
@storage || raise(Error, "Logsafe.storage is not set, see the README")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The names of the streams there are, if nothing else is to be merged, shown or encrypted again.
|
|
43
|
+
# Whoever may write parts can make up any number of other names.
|
|
44
|
+
def streams=(names)
|
|
45
|
+
names &&= Array(names).map(&:to_s)
|
|
46
|
+
wrong = names.to_a.grep_v(/\A#{STREAM}\z/)
|
|
47
|
+
raise Error, "Logsafe.streams has a name that no stream can have: #{wrong.first.inspect}" if wrong.any?
|
|
48
|
+
raise Error, "Logsafe.streams is empty. Leave it out to have every stream" if names == []
|
|
49
|
+
|
|
50
|
+
@streams = names
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def stream?(name)
|
|
54
|
+
@streams.nil? || @streams.include?(name)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# The key from `logsafe key`. With it the day's file is encrypted, as <stream>.<date>.log.gz.enc,
|
|
58
|
+
# and the lines a shipper encrypted with the same key can be read. Without it nothing is encrypted.
|
|
59
|
+
def key=(text)
|
|
60
|
+
@key = text && Cipher.key(text)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The keys that were in use before, as a list or in one text with commas between them. What they
|
|
64
|
+
# encrypted can still be read, and is encrypted with the key in use the next time it is written.
|
|
65
|
+
def old_keys=(texts)
|
|
66
|
+
@old_keys = Array(texts).flat_map { |text| text.scan(/[^\s,]+/) }.map { |text| Cipher.key(text) }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Every key that can decrypt: the one in use first. Old keys alone open nothing, or a key that
|
|
70
|
+
# went missing would have the merge write the days in plain text.
|
|
71
|
+
def keys
|
|
72
|
+
@key ? [@key, *@old_keys] : []
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def generate_key
|
|
76
|
+
Cipher.generate_key
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Turns a day's encrypted file back into its gzip.
|
|
80
|
+
def decrypt(from, into)
|
|
81
|
+
Cipher.unseal(from, into, keys)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Encrypts with the key in use what the old keys encrypted and what was never encrypted: every
|
|
85
|
+
# day's file and every part. When it has run without failing, the old keys can go.
|
|
86
|
+
# A day's plain file goes the way of a part: the merge takes it into the encrypted file, and its
|
|
87
|
+
# second run removes it, having found all of it there.
|
|
88
|
+
def reencrypt
|
|
89
|
+
Reencrypt.call(storage, @key || raise(Error, "Logsafe.key is not set"), @old_keys.to_a) do |stream, day|
|
|
90
|
+
2.times { Day.new(stream, day).merge([]) }
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Merges every finished day (UTC) of the given streams, or of all streams. Safe to run again at
|
|
95
|
+
# any time, though no more than every half hour where the page is in use: see Copies. One day that
|
|
96
|
+
# fails does not hold up the others. A run that
|
|
97
|
+
# has gone on for RUN leaves the rest to the next one, and because the order is another each time,
|
|
98
|
+
# those are not always the same.
|
|
99
|
+
def merge(*streams, today: Time.now.utc.strftime("%F"))
|
|
100
|
+
waiting = finished(streams, today).shuffle
|
|
101
|
+
stop = clock + RUN
|
|
102
|
+
failures = []
|
|
103
|
+
failures.concat(attempt(*waiting.pop)) while waiting.any? && clock < stop
|
|
104
|
+
warn "Logsafe leaves #{waiting.size} days to the next run: this one has taken long enough" if waiting.any?
|
|
105
|
+
raise failures.first if failures.any?
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# The parts among these keys, as { [stream, date] => keys }.
|
|
109
|
+
def parts(keys)
|
|
110
|
+
keys.group_by { |key| key.match(PART)&.captures }.select do |(stream, day), _|
|
|
111
|
+
day && stream?(stream) && Date.valid_date?(*day.split("-").map(&:to_i))
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
# Merges a day, and returns what went wrong, if anything did.
|
|
118
|
+
def attempt((stream, day), parts)
|
|
119
|
+
Day.new(stream, day).merge(parts)
|
|
120
|
+
[]
|
|
121
|
+
rescue StandardError => e
|
|
122
|
+
warn "Logsafe could not merge #{stream} #{day}: #{e.class}: #{e.message.lines.first}"
|
|
123
|
+
[e]
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def clock
|
|
127
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# [[stream, date], [keys of its parts]] for the days that are over.
|
|
131
|
+
def finished(streams, today)
|
|
132
|
+
parts = recent((Date.iso8601(today) - AGE).to_s)
|
|
133
|
+
parts.select { |(stream, day), _| day < today && (streams.empty? || streams.include?(stream)) }.to_a
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def recent(oldest)
|
|
137
|
+
keys = storage.keys(PARTS).reject { |key| key.end_with?("/") } # a folder, as some storages list them
|
|
138
|
+
parts = parts(keys).select { |(_, day), _| day >= oldest }
|
|
139
|
+
report(keys, keys.size - parts.values.sum(&:size))
|
|
140
|
+
parts
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# What is left alone is not lost, but it must be possible to notice.
|
|
144
|
+
def report(keys, strays)
|
|
145
|
+
return if strays.zero?
|
|
146
|
+
|
|
147
|
+
others = keys.filter_map { |key| key[PART, 1] }.uniq.reject { |stream| stream?(stream) }
|
|
148
|
+
warn "Logsafe leaves #{strays} files under #{PARTS} alone: not a stream's name, or not a date of the last year." \
|
|
149
|
+
"#{" Not in Logsafe.streams: #{others.first(5).join(", ")}" if others.any?}"
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
require_relative "logsafe/room"
|
|
155
|
+
require_relative "logsafe/private_folder"
|
|
156
|
+
require_relative "logsafe/storage"
|
|
157
|
+
require_relative "logsafe/cipher"
|
|
158
|
+
require_relative "logsafe/source"
|
|
159
|
+
require_relative "logsafe/day"
|
data/shipper/start
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Starts Vector so that what it writes to a folder is not for everyone to read, as Docker's own log
|
|
3
|
+
# files are not, and unless LOGSAFE_KEY is set to something that is not a key.
|
|
4
|
+
# Vector cannot refuse a key itself: it would start, and drop every line it could not encrypt.
|
|
5
|
+
# Stopping here loses nothing, because the lines wait in Docker's log files until Vector runs.
|
|
6
|
+
# docker run ... --entrypoint sh timberio/vector:0.62.0-alpine /etc/vector/start --config ...
|
|
7
|
+
if [ -n "${LOGSAFE_KEY:-}" ]; then
|
|
8
|
+
# Decoded and encoded again it must be itself: this base64 takes more than Vector's does.
|
|
9
|
+
bytes=$(printf %s "$LOGSAFE_KEY" | base64 -d 2>/dev/null | wc -c)
|
|
10
|
+
again=$(printf %s "$LOGSAFE_KEY" | base64 -d 2>/dev/null | base64)
|
|
11
|
+
if [ "$bytes" -ne 32 ] || [ "$again" != "$LOGSAFE_KEY" ]; then
|
|
12
|
+
echo "LOGSAFE_KEY is not a key. It must be 32 bytes in Base64, as \`logsafe key\` prints it," \
|
|
13
|
+
"with nothing around it: no quotes, no spaces. Vector was not started." >&2
|
|
14
|
+
exit 1
|
|
15
|
+
fi
|
|
16
|
+
fi
|
|
17
|
+
umask 027
|
|
18
|
+
exec vector "$@"
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# A folder: a disk on this server, a mounted volume or a network share.
|
|
2
|
+
# Mount the folder's parts/ into the Vector container at /logs/parts, and start Vector with `start`:
|
|
3
|
+
# what it writes is then not for everyone on the machine to read.
|
|
4
|
+
sinks:
|
|
5
|
+
storage:
|
|
6
|
+
type: file
|
|
7
|
+
inputs: [lines]
|
|
8
|
+
# A new file per server every minute, so that a file is soon finished: the merge job
|
|
9
|
+
# leaves a file alone until it has not changed for two minutes.
|
|
10
|
+
path: "/logs/parts/{{ stream }}/{{ day }}/{{ slot }}-{{ host }}.log"
|
|
11
|
+
encoding:
|
|
12
|
+
codec: text
|
|
13
|
+
framing:
|
|
14
|
+
method: newline_delimited
|
|
15
|
+
buffer:
|
|
16
|
+
type: disk # lines wait here while the storage can't be reached
|
|
17
|
+
max_size: 1073741824
|
|
18
|
+
when_full: block
|
|
19
|
+
acknowledgements:
|
|
20
|
+
enabled: true # a line counts as read only once it is safely in the buffer
|
data/shipper/to-gcs.yaml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Google Cloud Storage.
|
|
2
|
+
# The service account key comes from the file GOOGLE_APPLICATION_CREDENTIALS points to.
|
|
3
|
+
sinks:
|
|
4
|
+
storage:
|
|
5
|
+
type: gcp_cloud_storage
|
|
6
|
+
inputs: [lines]
|
|
7
|
+
bucket: my-logs
|
|
8
|
+
key_prefix: "parts/{{ stream }}/{{ day }}/"
|
|
9
|
+
encoding:
|
|
10
|
+
codec: text
|
|
11
|
+
framing:
|
|
12
|
+
method: newline_delimited
|
|
13
|
+
compression: gzip
|
|
14
|
+
batch:
|
|
15
|
+
timeout_secs: 60 # the most that is lost if this server's disk is destroyed
|
|
16
|
+
buffer:
|
|
17
|
+
type: disk # lines wait here while the storage can't be reached
|
|
18
|
+
max_size: 1073741824
|
|
19
|
+
when_full: block
|
|
20
|
+
acknowledgements:
|
|
21
|
+
enabled: true # a line counts as read only once it is safely in the buffer
|
data/shipper/to-s3.yaml
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Amazon S3, and anything that speaks its protocol: DigitalOcean Spaces, MinIO, Garage, ...
|
|
2
|
+
# The access key comes from AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
|
|
3
|
+
sinks:
|
|
4
|
+
storage:
|
|
5
|
+
type: aws_s3
|
|
6
|
+
inputs: [lines]
|
|
7
|
+
bucket: my-logs
|
|
8
|
+
region: us-east-1
|
|
9
|
+
# endpoint: https://ams3.digitaloceanspaces.com
|
|
10
|
+
key_prefix: "parts/{{ stream }}/{{ day }}/"
|
|
11
|
+
encoding:
|
|
12
|
+
codec: text
|
|
13
|
+
framing:
|
|
14
|
+
method: newline_delimited
|
|
15
|
+
compression: gzip
|
|
16
|
+
batch:
|
|
17
|
+
timeout_secs: 60 # the most that is lost if this server's disk is destroyed
|
|
18
|
+
buffer:
|
|
19
|
+
type: disk # lines wait here while the storage can't be reached
|
|
20
|
+
max_size: 1073741824
|
|
21
|
+
when_full: block
|
|
22
|
+
acknowledgements:
|
|
23
|
+
enabled: true # a line counts as read only once it is safely in the buffer
|
data/shipper/vector.yaml
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Ships the logs of labelled Docker containers on this server to your storage.
|
|
2
|
+
# Runs on every app server, together with ONE of to-s3.yaml, to-gcs.yaml or to-folder.yaml.
|
|
3
|
+
# The Logsafe merge job then makes one file per day.
|
|
4
|
+
#
|
|
5
|
+
# A container is shipped when it has a `logs` label and Docker is told to write
|
|
6
|
+
# that label into its log file:
|
|
7
|
+
# docker run --label logs=shop-prod-web --log-opt labels=logs ...
|
|
8
|
+
#
|
|
9
|
+
# To encrypt the lines, give Vector the key the app has in Logsafe.key, as LOGSAFE_KEY. Start Vector
|
|
10
|
+
# with the `start` script beside this file: it refuses a key that is not one.
|
|
11
|
+
|
|
12
|
+
data_dir: /var/lib/vector # where Vector remembers how far it has read, and buffers
|
|
13
|
+
|
|
14
|
+
sources:
|
|
15
|
+
docker:
|
|
16
|
+
type: file
|
|
17
|
+
# Docker's own log files, rotated ones included, so nothing is missed while Vector is down.
|
|
18
|
+
include: ["/var/lib/docker/containers/*/*-json.log*"]
|
|
19
|
+
max_line_bytes: 1048576 # Docker escapes a 16 KB line to up to ~100 KB; the default would drop it
|
|
20
|
+
|
|
21
|
+
transforms:
|
|
22
|
+
lines:
|
|
23
|
+
type: remap
|
|
24
|
+
inputs: [docker]
|
|
25
|
+
drop_on_error: true # a line that cannot be sealed is dropped, never shipped as it is; `start` keeps it from coming to that
|
|
26
|
+
source: |
|
|
27
|
+
entry = object(parse_json(.message) ?? null) ?? {}
|
|
28
|
+
line = entry.log
|
|
29
|
+
stream = entry.attrs.logs
|
|
30
|
+
if !is_string(line) { # not a line Docker wrote: keep it whole rather than lose it
|
|
31
|
+
line = .message
|
|
32
|
+
stream = "unparsed"
|
|
33
|
+
}
|
|
34
|
+
if !is_string(stream) { abort } # a container without the label
|
|
35
|
+
# The name becomes a folder under parts/: with a slash in it the merge would never find the
|
|
36
|
+
# lines, with dots in front they could land outside, and too long a name is too long for the
|
|
37
|
+
# day's file. Kept where they are seen instead.
|
|
38
|
+
if !match(string!(stream), r'^[A-Za-z0-9][A-Za-z0-9._-]{0,199}$') { stream = "unparsed" }
|
|
39
|
+
|
|
40
|
+
# The time Docker saw the line, at a fixed width, so sorting the text sorts by time.
|
|
41
|
+
at = parse_timestamp(entry.time, "%+") ?? now()
|
|
42
|
+
day = format_timestamp!(at, "%F")
|
|
43
|
+
message = format_timestamp!(at, "%Y-%m-%dT%H:%M:%S%.9fZ") + " " + replace(string!(line), r'\n$', "")
|
|
44
|
+
|
|
45
|
+
# With a key the whole line is sealed here, so the storage never holds it in plain text.
|
|
46
|
+
# Every stream and day gets a key of its own: so few lines share one that their random nonces
|
|
47
|
+
# are not expected to repeat.
|
|
48
|
+
secret = get_env_var("LOGSAFE_KEY") ?? ""
|
|
49
|
+
if secret != "" {
|
|
50
|
+
nonce = random_bytes(12)
|
|
51
|
+
master = decode_base64!(secret)
|
|
52
|
+
assert!(length(master) == 32, "LOGSAFE_KEY is not a key") # hmac would take any, and the app could not read the line
|
|
53
|
+
key = hmac(string!(stream) + "/" + day, master)
|
|
54
|
+
message = "enc:" + encode_base64(nonce + encrypt!(message, "CHACHA20-POLY1305", key, nonce))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
. = {
|
|
58
|
+
"stream": stream,
|
|
59
|
+
"day": day,
|
|
60
|
+
"message": message,
|
|
61
|
+
# For to-folder.yaml, which starts a new file per server every minute.
|
|
62
|
+
"host": get_hostname() ?? "server",
|
|
63
|
+
"slot": format_timestamp!(now(), "%Y%m%dT%H%M")
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
tests:
|
|
67
|
+
- name: a line from a labelled container
|
|
68
|
+
inputs:
|
|
69
|
+
- insert_at: lines
|
|
70
|
+
type: log
|
|
71
|
+
log_fields:
|
|
72
|
+
message: '{"log":"Started GET /\n","stream":"stdout","attrs":{"logs":"shop-prod-web"},"time":"2026-11-24T23:59:59.5Z"}'
|
|
73
|
+
outputs:
|
|
74
|
+
- extract_from: lines
|
|
75
|
+
conditions:
|
|
76
|
+
- type: vrl
|
|
77
|
+
source: |
|
|
78
|
+
assert_eq!(.message, "2026-11-24T23:59:59.500000000Z Started GET /")
|
|
79
|
+
assert_eq!(.stream, "shop-prod-web")
|
|
80
|
+
assert_eq!(.day, "2026-11-24")
|
|
81
|
+
- name: a container without the label is not shipped
|
|
82
|
+
inputs:
|
|
83
|
+
- insert_at: lines
|
|
84
|
+
type: log
|
|
85
|
+
log_fields:
|
|
86
|
+
message: '{"log":"proxy noise\n","stream":"stdout","time":"2026-11-24T10:00:00Z"}'
|
|
87
|
+
no_outputs_from: [lines]
|
|
88
|
+
- name: a label that cannot be a folder's name is not used as one
|
|
89
|
+
inputs:
|
|
90
|
+
- insert_at: lines
|
|
91
|
+
type: log
|
|
92
|
+
log_fields:
|
|
93
|
+
message: '{"log":"hello\n","stream":"stdout","attrs":{"logs":"../team/web"},"time":"2026-11-24T10:00:00Z"}'
|
|
94
|
+
outputs:
|
|
95
|
+
- extract_from: lines
|
|
96
|
+
conditions:
|
|
97
|
+
- type: vrl
|
|
98
|
+
source: |
|
|
99
|
+
assert_eq!(.stream, "unparsed")
|
|
100
|
+
assert_eq!(.message, "2026-11-24T10:00:00.000000000Z hello")
|
|
101
|
+
- name: a line that is not Docker's is kept
|
|
102
|
+
inputs:
|
|
103
|
+
- insert_at: lines
|
|
104
|
+
type: log
|
|
105
|
+
log_fields:
|
|
106
|
+
message: '{"log":"cut off in the mid'
|
|
107
|
+
outputs:
|
|
108
|
+
- extract_from: lines
|
|
109
|
+
conditions:
|
|
110
|
+
- type: vrl
|
|
111
|
+
source: |
|
|
112
|
+
assert_eq!(.stream, "unparsed")
|
|
113
|
+
assert!(ends_with(string!(.message), "Z {\"log\":\"cut off in the mid"))
|