This file is indexed.

/usr/share/cfengine3/masterfiles/lib/vcs.cf is in cfengine3 3.10.2-4build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
############################################################################
#  Copyright 2017 Northern.tech AS
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License LGPL as published by the
#  Free Software Foundation; version 3.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  To the extent this program is licensed as part of the Enterprise
#  versions of CFEngine, the applicable Commercial Open Source License
#  (COSL) may apply to this file if you as a licensee so wish it. See
#  included file COSL.txt.
###########################################################################
#
# CFEngine Community Open Promise-Body Library
#
# This initiative started by CFEngine promotes a
# standardized set of names and promise specifications
# for template functionality within CFEngine 3.
#
# The aim is to promote an industry standard for
# naming of configuration patterns, leading to a
# de facto middleware of standardized syntax.
#
# Names should be intuitive and parameters should be
# minimal to assist readability and comprehensibility.

# Contributions to this file are voluntarily given to
# the cfengine community, and are moderated by CFEngine.
# No liability or warranty for misuse is implied.
#
# If you add to this file, please try to make the
# contributions "self-documenting". Comments made
# after the bundle/body statement are retained in
# the online docs
#

# For CFEngine Core: 3.7.0 to 3.7.x
# VCS Bundles

###################################################
# If you find CFEngine useful, please consider    #
# purchasing a commercial version of the software.#
###################################################

bundle common vcs_common
# @ignore
{
  vars:
      "inputs" slist => { "$(this.promise_dirname)/common.cf",
                          "$(this.promise_dirname)/paths.cf",
                          "$(this.promise_dirname)/commands.cf" };
}

body file control
# @ignore
{
      inputs => { @(vcs_common.inputs) };
}

bundle agent git_init(repo_path)
# @brief initializes a new git repository if it does not already exist
# @depends git
# @param repo_path absolute path of where to initialize a git repository
#
# **Example:**
#
# ```cf3
# bundle agent my_git_repositories
# {
#   vars:
#     "basedir"  string => "/var/git";
#     "repos"    slist  => { "myrepo", "myproject", "myPlugForMoreHaskell" };
#
#   files:
#     "$(basedir)/$(repos)/."
#       create => "true";
#
#   methods:
#     "git_init" usebundle => git_init("$(basedir)/$(repos)");
# }
# ```
{
  classes:
    "ok_norepo" not => fileexists("$(repo_path)/.git");

  methods:
    ok_norepo::
      "git_init"  usebundle => git("$(repo_path)", "init", "");
}

bundle agent git_add(repo_path, file)
# @brief adds files to the supplied repository's index
# @depends git
# @param repo_path absolute path to a git repository
# @param file a file to stage in the index
#
# **Example:**
#
# ```cf3
# bundle agent add_files_to_git_index
# {
#   vars:
#     "repo"  string => "/var/git/myrepo";
#     "files" slist  => { "fileA", "fileB", "fileC" };
#
#   methods:
#     "git_add" usebundle => git_add("$(repo)", "$(files)");
# }
# ```
{
  classes:
    "ok_repo" expression => fileexists("$(repo_path)/.git");

  methods:
    ok_repo::
      "git_add" usebundle => git("$(repo_path)", "add", "$(file)");
}

bundle agent git_checkout(repo_path, branch)
# @brief checks out an existing branch in the supplied git repository
# @depends git
# @param repo_path absolute path to a git repository
# @param branch the name of an existing git branch to checkout
#
# **Example:**
#
# ```cf3
# bundle agent git_checkout_some_existing_branch
# {
#   vars:
#     "repo"   string => "/var/git/myrepo";
#     "branch" string => "dev/some-topic-branch";
#
#   methods:
#     "git_checkout" usebundle => git_checkout("$(repo)", "$(branch)");
# }
# ```
{
  classes:
    "ok_repo" expression => fileexists("$(repo_path)/.git");

  methods:
    ok_repo::
      "git_checkout" usebundle => git("$(repo_path)", "checkout", "$(branch)");
}

bundle agent git_checkout_new_branch(repo_path, new_branch)
# @brief checks out and creates a new branch in the supplied git repository
# @depends git
# @param repo_path absolute path to a git repository
# @param new_branch the name of the git branch to create and checkout
#
# **Example:**
#
# ```cf3
# bundle agent git_checkout_new_branches
# {
#   vars:
#     "repo[myrepo]"    string => "/var/git/myrepo";
#     "branch[myrepo]"  string => "dev/some-new-topic-branch";
#
#     "repo[myproject]"   string => "/var/git/myproject";
#     "branch[myproject]" string => "dev/another-new-topic-branch";
#
#     "repo_names"        slist => getindices("repo");
#
#   methods:
#     "git_checkout_new_branch" usebundle => git_checkout_new_branch("$(repo[$(repo_names)])", "$(branch[$(repo_names)])");
# }
# ```
{
  classes:
    "ok_repo" expression => fileexists("$(repo_path)/.git");

  methods:
    ok_repo::
      "git_checkout" usebundle => git("$(repo_path)", "checkout -b", "$(branch)");
}

bundle agent git_clean(repo_path)
# @brief Ensure that a given git repo is clean
# @param repo_path Path to the clone
#
# **Example:**
#
# ```cf3
#  methods:
#    "test"
#      usebundle => git_clean("/opt/cfengine/masterfiles_staging_tmp"),
#      comment => "Ensure that the staging area is a clean clone";
# ```
{
  methods:
      "" usebundle => git("$(repo_path)", "clean", ' --force -d'),
      comment => "To have a clean clone we must remove any untracked files and
                  directories. These should have all been stashed, but in case
                  of error we go ahead and clean anyway.";
}

bundle agent git_stash(repo_path, stash_name)
# @brief Stash any changes (including untracked files) in repo_path
# @param repo_path Path to the clone
# @param stash_name Stash name
#
# **Example:**
#
# ```cf3
#  methods:
#    "test"
#      usebundle => git_stash("/opt/cfengine/masterfiles_staging_tmp", "temp"),
#      comment => "Stash any changes, including untracked files";
# ```
{
  methods:
      "" usebundle => git($(repo_path), "stash", 'save --quiet --include-untracked "$(stash_name)"'),
      comment => "So that we don't lose any trail of what happened and so that
                    we don't accidentally delete something important we stash any
                    changes.
  Note:
                      1. This promise will fail if user.email is not set
                      2. We are respecting ignored files.";
}

bundle agent git_stash_and_clean(repo_path)
# @brief Ensure that a given git repo is clean and attempt to save any modifications
# @param repo_path Path to the clone
#
# **Example:**
#
# ```cf3
#  methods:
#    "test"
#      usebundle => git_stash_and_clean("/opt/cfengine/masterfiles_staging_tmp"),
#      comment => "Ensure that the staging area is a clean clone after attempting to stash any changes";
# ```
{
  vars:
      "stash" string => "CFEngine AUTOSTASH: $(sys.date)";

  methods:
      "" usebundle => git_stash($(repo_path), $(stash)),
      classes => scoped_classes_generic("bundle", "git_stash");

    git_stash_ok::
      "" usebundle => git_clean($(repo_path));

  reports:
    git_stash_not_ok::
      "$(this.bundle):: Warning: Not saving changes or cleaning. Git stash failed. Perhaps 'user.email' or 'user.name' is not set.";
}

bundle agent git_commit(repo_path, message)
# @brief executes a commit to the specificed git repository
# @depends git
# @param repo_path absolute path to a git repository
# @param message the message to associate to the commmit
#
# **Example:**
#
# ```cf3
# bundle agent make_git_commit
# {
#   vars:
#     "repo"  string => "/var/git/myrepo";
#     "msg"   string => "dituri added some bundles for common git operations";
#
#   methods:
#     "git_commit" usebundle => git_commit("$(repo)", "$(msg)");
# }
# ```
{
  classes:
    "ok_repo" expression => fileexists("$(repo_path)/.git");

  methods:
    ok_repo::
      "git_commit" usebundle => git("$(repo_path)", "commit", '-m "$(message)"');
}

bundle agent git(repo_path, subcmd, args)
# @brief generic interface to git
# @param repo_path absolute path to a new or existing git repository
# @param subcmd any valid git sub-command
# @param args a single string of arguments to pass
#
# This bundle will drop privileges if running as root (uid 0) and the
# repository is owned by a different user. Use `DEBUG` or `DEBUG_git` (from the
# command line, `-D DEBUG_git`) to see every Git command it runs.
#
# **Example:**
#
# ```cf3
# bundle agent git_rm_files_from_staging
# {
#   vars:
#     "repo"        string => "/var/git/myrepo";
#     "git_cmd"     string => "reset --soft";
#     "files"       slist  => { "fileA", "fileB", "fileC" };
#
#   methods:
#     "git_reset" usebundle => git("$(repo)", "$(git_cmd)", "HEAD -- $(files)");
# }
# ```
{
  vars:
      "oneliner" string => "$(paths.path[git])";

      "repo_uid"
      string  => filestat($(repo_path), "uid"),
      comment => "So that we don't mess up permissions, we will just execute
                    all commands as the current owner of .git";

      "repo_gid"
      string  => filestat($(repo_path), "gid"),
      comment => "So that we don't mess up permissions, we will just execute
                    all commands as the current group of .git";

      # We get the passwd entry from the user that owns the repo so
      # that we can extract the home directory for later use.
      "repo_uid_passwd_ent"
        string => execresult("$(paths.getent) passwd $(repo_uid)", noshell),
        comment => "We need to extract the home directory of the repo
                    owner so that it can be used to avoid errors from
                    unprivledged execution trying to access the root
                    users git config.";

  classes:
      "am_root" expression => strcmp($(this.promiser_uid), "0");

      # $(repo_uid) must be defined before we try to test this or we will end up
      # having at least one pass during evaluation the agent will not know it
      # needs to drop privileges, leading to some files like .git/index being
      # created with elevated privileges, and subsequently causing the agent to
      # not be able to commit as a normal user.
      "need_to_drop"
        not => strcmp($(this.promiser_uid), $(repo_uid)),
        ifvarclass => isvariable( repo_uid );

    am_root.need_to_drop::
      # This regular expression could be tightened up
      # Extract the home directory from the owner of the repository
      # into $(repo_uid_passwd[1])
      "extracted_repo_uid_home"
        expression => regextract( ".*:.*:\d+:\d+:.*:(.*):.*",
                                  $(repo_uid_passwd_ent),
                                  "repo_uid_passwd" ),
        ifvarclass => isvariable("repo_uid_passwd_ent");

  commands:
    am_root.need_to_drop::
      # Because cfengine does not inherit the shell environment when
      # executing commands, git will look for the root users git
      # config and error when the executing user does not have
      # access. So we need to set the home directory of the executing
      # user.
      "$(paths.env) HOME=$(repo_uid_passwd[1]) $(oneliner)"
        args => "$(subcmd) $(args)",
        classes => kept_successful_command,
        contain => setuidgid_dir( $(repo_uid), $(repo_gid), $(repo_path) );

    !am_root|!need_to_drop::
      "$(oneliner)"
      args => "$(subcmd) $(args)",
      classes => kept_successful_command,
      contain => in_dir( $(repo_path) );

  reports:
    "DEBUG|DEBUG_$(this.bundle).am_root.need_to_drop"::
      "DEBUG $(this.bundle): with dropped privileges to uid '$(repo_uid)' and gid '$(repo_gid)', in directory '$(repo_path)', running Git command '$(paths.env) HOME=\"$(repo_uid_passwd[1])\" $(oneliner) $(subcmd) $(args)'"
        ifvarclass => isvariable("repo_uid_passwd[1]");

    "DEBUG|DEBUG_$(this.bundle).(!am_root|!need_to_drop)"::
      "DEBUG $(this.bundle): with current privileges, in directory '$(repo_path)', running Git command '$(oneliner) $(subcmd) $(args)'";
}