try a new approach to read node config (#12977)

This commit is contained in:
Roberto Dip 2023-07-26 14:16:53 -03:00 committed by GitHub
parent 1106c78ba4
commit 4940a5e186
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 24 deletions

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
require 'puppet/util/fleet_client'
require_relative '../../util/fleet_client'
# fleetdm::command_xml sends a custom MDM command to the device
# with the provided UUID.
@ -14,10 +14,9 @@ Puppet::Functions.create_function(:"fleetdm::command_xml") do
end
def command_xml(uuid, xml_data)
host = call_function('lookup', 'fleetdm::host')
token = call_function('lookup', 'fleetdm::token')
client = Puppet::Util::FleetClient.new(host, token)
response = client.send_mdm_command(uuid, xml_data)
env = closure_scope['server_facts']['environment']
client = Puppet::Util::FleetClient.instance
response = client.send_mdm_command(uuid, xml_data, env)
if response['error'].empty?
Puppet.info('Successfully sent custom MDM command')

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
require 'puppet/util/fleet_client'
require_relative '../../util/fleet_client'
Puppet::Functions.create_function(:"fleetdm::preassign_profile") do
dispatch :preassign_profile do
@ -13,13 +13,14 @@ Puppet::Functions.create_function(:"fleetdm::preassign_profile") do
def preassign_profile(profile_identifier, host_uuid, template, group = 'default', ensure_profile = 'present')
client = Puppet::Util::FleetClient.instance
env = closure_scope['server_facts']['environment']
run_identifier = "#{closure_scope.catalog.catalog_uuid}-#{Puppet[:node_name_value]}"
response = client.preassign_profile(run_identifier, host_uuid, template, group, ensure_profile)
response = client.preassign_profile(run_identifier, host_uuid, template, group, ensure_profile, closure_scope['environment'])
if response['error'].empty?
base64_checksum = Digest::MD5.base64digest(template)
host = client.get_host_by_identifier(host_uuid)
host_profiles = client.get_host_profiles(host['body']['host']['id'])
host = client.get_host_by_identifier(host_uuid, env)
host_profiles = client.get_host_profiles(host['body']['host']['id'], env)
if host_profiles['error'].empty?
Puppet.info("successfully pre-set profile #{profile_identifier} as #{ensure_profile}")

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
require 'puppet/util/fleet_client'
require_relative '../../util/fleet_client'
# fleetdm::release_device sends the [`DeviceConfigured`][1] MDM command to the
# device with the provided UUID. This is useful to release DEP enrolled devices
@ -29,8 +29,9 @@ Puppet::Functions.create_function(:"fleetdm::release_device") do
</plist>
COMMAND_TEMPLATE
env = closure_scope['server_facts']['environment']
client = Puppet::Util::FleetClient.instance
response = client.send_mdm_command(uuid, command_xml)
response = client.send_mdm_command(uuid, command_xml, env)
if response['error'].empty?
Puppet.info('successfully released device')

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'puppet'
require 'puppet/util/fleet_client'
require_relative '../util/fleet_client'
Puppet::Reports.register_report(:fleetdm) do
desc 'Used to signal the Fleet server that a Puppet run is done to match a device to a team for profile delivery'
@ -11,7 +11,7 @@ Puppet::Reports.register_report(:fleetdm) do
client = Puppet::Util::FleetClient.instance
node_name = Puppet[:node_name_value]
run_identifier = "#{catalog_uuid}-#{node_name}"
response = client.match_profiles(run_identifier)
response = client.match_profiles(run_identifier, environment)
if response['error'].empty?
Puppet.info("successfully matched #{node_name} with a team containing configuration profiles")

View file

@ -37,7 +37,7 @@ module Puppet::Util
# @param profile_xml [String] Raw XML with the configuration profile.
# @param group [String] Used to construct a team name.
# @return [Hash] The response status code, headers, and body.
def preassign_profile(run_identifier, uuid, profile_xml, group, ensure_profile)
def preassign_profile(run_identifier, uuid, profile_xml, group, ensure_profile, environment)
req(
method: :post,
path: '/api/latest/fleet/mdm/apple/profiles/preassign',
@ -48,6 +48,7 @@ module Puppet::Util
'group' => group,
'exclude' => ensure_profile == 'absent',
},
environment: environment,
)
end
@ -60,11 +61,12 @@ module Puppet::Util
# @param run_identifier [String] Used to identify this run to match
# pre-assigned profiles.
# @return [Hash] The response status code, headers, and body.
def match_profiles(run_identifier)
def match_profiles(run_identifier, environment)
req(
method: :post,
path: '/api/latest/fleet/mdm/apple/profiles/match',
body: { 'external_host_identifier' => run_identifier },
environment: environment,
)
end
@ -73,7 +75,7 @@ module Puppet::Util
# @param uuid [String] The host uuid.
# @param command_xml [String] Raw XML with the MDM command.
# @return [Hash] The response status code, headers, and body.
def send_mdm_command(uuid, command_xml)
def send_mdm_command(uuid, command_xml, environment)
req(method: :post, path: '/api/latest/fleet/mdm/apple/enqueue',
body: {
# For some reason, the enqueue function expects the command to be
@ -84,15 +86,21 @@ module Puppet::Util
# removing the padding manually instead.
'command' => Base64.strict_encode64(command_xml).gsub(%r{[\n=]}, ''),
'device_ids' => [uuid],
})
},
environment: environment)
end
# Get profiles assigned to the host.
#
# @param host_id [Number] Fleet's internal host id.
# @return [Hash] The response status code, headers, and body.
def get_host_profiles(host_id)
req(method: :get, path: "/api/latest/fleet/mdm/hosts/#{host_id}/profiles", cached: false)
def get_host_profiles(host_id, environment)
req(
method: :get,
path: "/api/latest/fleet/mdm/hosts/#{host_id}/profiles",
cached: false,
environment: environment,
)
end
# Gets host details by host identifier.
@ -100,16 +108,21 @@ module Puppet::Util
# @param identifier [String] The host identifier, can be
# osquery_host_identifier, node_key, UUID, or hostname.
# @return [Hash] The response status code, headers, and body.
def get_host_by_identifier(identifier)
req(method: :get, path: "/api/latest/fleet/hosts/identifier/#{identifier}", cached: true)
def get_host_by_identifier(identifier, environment)
req(
method: :get,
path: "/api/latest/fleet/hosts/identifier/#{identifier}",
cached: true,
environment: environment,
)
end
private
def req(method: :get, path: '', body: nil, headers: {}, cached: false)
def req(method: :get, path: '', body: nil, headers: {}, cached: false, environment: 'production')
node_name = Puppet[:node_name_value]
node = Puppet::Node.new(node_name)
node.environment = Puppet.lookup(:current_environment).name.to_s
node.environment = environment
compiler = Puppet::Parser::Compiler.new(node)
scope = Puppet::Parser::Scope.new(compiler)
lookup_invocation = Puppet::Pops::Lookup::Invocation.new(scope, {}, {}, nil)

View file

@ -1,6 +1,6 @@
{
"name": "fleetdm-fleetdm",
"version": "0.2.2",
"version": "0.2.3",
"author": "Fleet Device Management Inc",
"summary": "MDM management and profile assignment using FleetDM",
"license": "proprietary",