summaryrefslogtreecommitdiffstats
path: root/lib/puppet/indirector/file_server.rb
blob: 5fe744a0e43a13a258f22d7db56ea1ba44e18d4c (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
#
#  Created by Luke Kanies on 2007-10-19.
#  Copyright (c) 2007. All rights reserved.

require 'puppet/file_serving/configuration'
require 'puppet/file_serving/fileset'
require 'puppet/file_serving/terminus_helper'
require 'puppet/indirector/terminus'

# Look files up using the file server.
class Puppet::Indirector::FileServer < Puppet::Indirector::Terminus
    include Puppet::FileServing::TerminusHelper

    # Is the client authorized to perform this action?
    def authorized?(request)
        return false unless [:find, :search].include?(request.method)

        mount, file_path = configuration.split_path(request)

        # If we're not serving this mount, then access is denied.
        return false unless mount
        return mount.allowed?(request.node, request.ip)
    end

    # Find our key using the fileserver.
    def find(request)
        mount, relative_path = configuration.split_path(request)

        return nil unless mount

        # The mount checks to see if the file exists, and returns nil
        # if not.
        return nil unless path = mount.find(relative_path, :node => request.node)
        result = model.new(path)
        result.links = request.options[:links] if request.options[:links]
        result.collect
        result
    end

    # Search for files.  This returns an array rather than a single
    # file.
    def search(request)
        mount, relative_path = configuration.split_path(request)

        return nil unless mount

        return nil unless paths = mount.search(relative_path, :node => request.node)

        filesets = paths.collect do |path|
            # Filesets support indirector requests as an options collection
            Puppet::FileServing::Fileset.new(path, request)
        end

        Puppet::FileServing::Fileset.merge(*filesets).collect do |file, base_path|
            inst = model.new(base_path, :relative_path => file)
            inst.links = request.options[:links] if request.options[:links]
            inst.collect
            inst
        end
    end

    private

    # Our fileserver configuration, if needed.
    def configuration
        Puppet::FileServing::Configuration.create
    end
end