aboutsummaryrefslogtreecommitdiff
path: root/mod/build-result-module.cxx
blob: 7823e3a82f44f58c7e8374780ce38700fe9173de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// file      : mod/build-result-module.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <mod/build-result-module.hxx>

#include <libbutl/openssl.hxx>
#include <libbutl/process-io.hxx>

namespace brep
{
  using namespace std;
  using namespace butl;

  // While currently the user-defined copy constructor is not required (we
  // don't need to deep copy nullptr's), it is a good idea to keep the
  // placeholder ready for less trivial cases.
  //
  build_result_module::
  build_result_module (const build_result_module& r)
      : database_module (r),
        build_config_module (r),
        use_openssl_pkeyutl_ (r.initialized_ ? r.use_openssl_pkeyutl_ : false)
  {
  }

  void build_result_module::
  init (const options::build& bo, const options::build_db& bdo)
  {
    HANDLER_DIAG;

    build_config_module::init (bo);
    database_module::init (bdo, bdo.build_db_retry ());

    try
    {
      optional<openssl_info> oi (
        openssl::info ([&trace, this] (const char* args[], size_t n)
                       {
                         l2 ([&]{trace << process_args {args, n};});
                       },
                       2,
                       bo.openssl ()));

      use_openssl_pkeyutl_ = oi                    &&
                             oi->name == "OpenSSL" &&
                             oi->version >= semantic_version {3, 0, 0};
    }
    catch (const system_error& e)
    {
      fail << "unable to obtain openssl version: " << e;
    }
  }

  build_result_module::parse_session_result build_result_module::
  parse_session (const string& s) const
  {
    using brep::version; // Not to confuse with module::version.

    parse_session_result r;

    size_t p (s.find ('/')); // End of tenant.

    if (p == string::npos)
      throw invalid_argument ("no package name");

    if (tenant.compare (0, tenant.size (), s, 0, p) != 0)
      throw invalid_argument ("tenant mismatch");

    size_t b (p + 1);    // Start of package name.
    p = s.find ('/', b); // End of package name.

    if (p == b)
      throw invalid_argument ("empty package name");

    if (p == string::npos)
      throw invalid_argument ("no package version");

    package_name name;

    try
    {
      name = package_name (string (s, b, p - b));
    }
    catch (const invalid_argument& e)
    {
      throw invalid_argument (
        string ("invalid package name : ") + e.what ());
    }

    b = p + 1;           // Start of version.
    p = s.find ('/', b); // End of version.

    if (p == string::npos)
      throw invalid_argument ("no target");

    auto parse_version = [&s, &b, &p] (const char* what) -> version
    {
      // Intercept exception handling to add the parsing error attribution.
      //
      try
      {
        return brep::version (string (s, b, p - b));
      }
      catch (const invalid_argument& e)
      {
        throw invalid_argument (
          string ("invalid ") + what + ": " + e.what ());
      }
    };

    r.package_version = parse_version ("package version");

    b = p + 1;           // Start of target.
    p = s.find ('/', b); // End of target.

    if (p == string::npos)
      throw invalid_argument ("no target configuration name");

    target_triplet target;
    try
    {
      target = target_triplet (string (s, b, p - b));
    }
    catch (const invalid_argument& e)
    {
      throw invalid_argument (string ("invalid target: ") + e.what ());
    }

    b = p + 1;           // Start of target configuration name.
    p = s.find ('/', b); // End of target configuration name.

    if (p == string::npos)
      throw invalid_argument ("no package configuration name");

    string target_config (s, b, p - b);

    if (target_config.empty ())
      throw invalid_argument ("empty target configuration name");

    b = p + 1;           // Start of package configuration name.
    p = s.find ('/', b); // End of package configuration name.

    if (p == string::npos)
      throw invalid_argument ("no toolchain name");

    string package_config (s, b, p - b);

    if (package_config.empty ())
      throw invalid_argument ("empty package configuration name");

    b = p + 1;           // Start of toolchain name.
    p = s.find ('/', b); // End of toolchain name.

    if (p == string::npos)
      throw invalid_argument ("no toolchain version");

    string toolchain_name (s, b, p - b);

    if (toolchain_name.empty ())
      throw invalid_argument ("empty toolchain name");

    b = p + 1;           // Start of toolchain version.
    p = s.find ('/', b); // End of toolchain version.

    if (p == string::npos)
      throw invalid_argument ("no timestamp");

    r.toolchain_version = parse_version ("toolchain version");

    r.id = build_id (package_id (move (tenant), move (name), r.package_version),
                     move (target),
                     move (target_config),
                     move (package_config),
                     move (toolchain_name),
                     r.toolchain_version);

    try
    {
      size_t tsn;
      string ts (s, p + 1);

      r.timestamp = timestamp (chrono::duration_cast<timestamp::duration> (
                                 chrono::nanoseconds (stoull (ts, &tsn))));

      if (tsn != ts.size ())
        throw invalid_argument ("trailing junk");
    }
    // Handle invalid_argument or out_of_range (both derive from logic_error),
    // that can be thrown by stoull().
    //
    catch (const logic_error& e)
    {
      throw invalid_argument (string ("invalid timestamp: ") + e.what ());
    }

    return r;
  }

  bool build_result_module::
  authenticate_session (const options::build& o,
                        const optional<vector<char>>& challenge,
                        const build& b,
                        const string& session) const
  {
    HANDLER_DIAG;

    auto warn_auth = [&session, &warn] (const string& d)
    {
      warn << "session '" << session << "' authentication failed: " << d;
    };

    bool r (false);

    // Must both be present or absent.
    //
    if (!b.agent_challenge != !challenge)
    {
      warn_auth (challenge ? "unexpected challenge": "challenge is expected");
    }
    else if (bot_agent_key_map_ == nullptr) // Authentication is disabled.
    {
      r = true;
    }
    else if (!b.agent_challenge) // Authentication is recently enabled.
    {
      warn_auth ("challenge is required now");
    }
    else
    {
      assert (b.agent_fingerprint && challenge);
      auto i (bot_agent_key_map_->find (*b.agent_fingerprint));

      // The agent's key is recently replaced.
      //
      if (i == bot_agent_key_map_->end ())
      {
        warn_auth ("agent's public key not found");
      }
      else
      try
      {
        openssl os ([&trace, this] (const char* args[], size_t n)
                    {
                      l2 ([&]{trace << process_args {args, n};});
                    },
                    path ("-"), fdstream_mode::text, 2,
                    process_env (o.openssl (), o.openssl_envvar ()),
                    use_openssl_pkeyutl_ ? "pkeyutl" : "rsautl",
                    o.openssl_option (),
                    use_openssl_pkeyutl_ ? "-verifyrecover" : "-verify",
                    "-pubin",
                    "-inkey",
                    i->second);

        for (const auto& c: *challenge)
          os.out.put (c); // Sets badbit on failure.

        os.out.close ();

        string s;
        getline (os.in, s);

        bool v (os.in.eof ());
        os.in.close ();

        if (os.wait () && v)
        {
          r = (s == *b.agent_challenge);

          if (!r)
            warn_auth ("challenge mismatched");
        }
        else // The signature is presumably meaningless.
          warn_auth ("unable to verify challenge");
      }
      catch (const system_error& e)
      {
        fail << "unable to verify challenge: " << e;
      }
    }

    return r;
  }
}