VMware Cloud Foundation (VCF) Operations checks my lab against its compliance rules and shows me what has drifted. That’s fine in the UI, but I wanted to do the same from a script: pick one setting, check it, fix it, check again. In VCF 9.1 that work is done by Salt, and Salt comes with an API, the VMware Salt for VCF Components API. This post is what I worked out while using it to fix NTP on six ESX hosts. Some of it is in the docs. Some of it I had to dig out of the job history.

By the end you’ll have:

  • A bearer token for the API
  • A few read-only calls to look around (minions, Salt environments, files, and jobs)
  • An NTP spec for ESX, drafted with a community generator and pushed to the Salt file server
  • An assessment and a remediation run against the ESX hosts behind a vCenter

Where this API fits

The configuration compliance side of VCF, Security Posture Management (SPM) in VCF Operations, is powered by Salt. When VCF Operations assesses your environment, it doesn’t log in to vCenter or NSX itself. It sends Salt jobs to a RaaS server and Salt minions running on the VCF components perform assessment and remediation. The Salt for VCF Components API is the API of that RaaS server, so whatever SPM does, you can do yourself.

A few things that helped me get a picture of it:

  • RaaS runs as pods in the VSP Kubernetes cluster, in the salt-raas namespace. You reach it through the fleet LCM FQDN at /raas/rpc. The docs write this as https://<vcfms-hostname>/raas/rpc; in my lab it is fleetlcm.gpu.set.lab.
  • Minions run on the components themselves: vCenter, the NSX Managers, SDDC Manager, the VCF Operations nodes, and the cloud proxy, plus one minion pod inside VSP.
  • ESX hosts have no minions. The vCenter minion checks and fixes them through vCenter. That decides how you target everything later.
  • The compliance content lives in a Salt environment called vcfsecops, not in the usual base.
  • The nightly assessment is a state.apply of a state called ops_assess_u, with a pillar that specifies which objects to check and the desired values. You can read those jobs using the API, and that was the most useful thing I found.

Authentication

There are three hops: an API token from VCF Operations, an exchange at the VCF instance’s Identity Broker (vIDB) for a bearer token, and then providing that bearer token on every RaaS call.

1. Create an API client in VCF Operations

Prerequisite: VCF SSO must be enabled and configured before you can generate an API token.

In VCF Operations go to Manage > Fleet Management > Identity and Access > Identity Broker > API Access. Create an API client with the Config Admin role (VCF Administrator includes it by default), then generate a token in the UI and copy it. It starts with vidb_. Treat it like a password.

2. Exchange it for a bearer token

The vIDB token endpoint takes the API token with a custom grant type and hands back an OAuth access token:

CUSTOMER in the path is the tenant, and it worked as-is for me. The access token is short-lived, so when calls start failing on auth, run the exchange again. I use -k only because my lab runs on self-signed certificates.

3. Call RaaS

Every call is a POST to /raas/rpc with a JSON body that names a resource, a method, and its keyword arguments. A small shell helper saves a lot of typing:

Pass it a JSON string, or @file.json to send a file. The response always has the same envelope:

ret holds the result and error tells you what went wrong. These are the error codes I ran into:

CodeMessageWhat it meant for me
3001RPC func ‘<resource>.<method>’ was not foundThe method doesn’t exist on your build. The “latest” docs can be ahead of what you run.
3004Request validation failure (the detail lists the bad fields)The method exists, you’re just missing arguments. Handy for checking a method without changing anything.
5000Internal ErrorSomething broke on the server. Read the RaaS pod logs.

For a 5000, or when calls just hang, the RaaS logs tell you what happened. I ran these on a VSP node:

I also keep all of these calls in a VS Code REST Client file, which is nicer for clicking through responses. The token request gets a name, and the other requests reuse its result:

The whole REST Client file is in the GitHub repo for this post:
https://github.com/denysaleksandrov/vfc-salt-api-test

Looking around

Before changing anything, I want to know what’s connected, which files are on the Salt side, and what has been running. All of the calls in this section are read-only.

Which minions are present

Presence shows which minions are connected, with the minion ID next to the host name:

ret.get_minions lists the minion IDs grouped by Salt master:

minions.get_minion_details returns the grains of every minion when you call it without arguments. Two grains matter most here: fqdn, and vcfops_resource_kind, which says what the component is:

You can also filter on a grain, and list the values a grain has across all minions:

In my lab the kinds are vcenter, nsxm, ops, sddcm, cloud-proxy and vmsp. No ESX hosts, as expected.

Salt environments and files

On my system that returns ["vcfsecops"]. To list the files in it:

You’ll see ops_assess_u.sls, ops_remed_u.sls, _states/ops.py and a set of _utils/*.py files, one per product. To read a file use fs.get_file. The contents come back base64-encoded unless the file was stored as text/plain:

That’s the state VCF Operations applies for its assessments, and it’s worth reading:

It syncs the custom modules from vcfsecops, installs the config-modules package where a product needs one, and then calls vmware_compliance_control.check_control. 

Everything comes from the pillar: object_type, object_id and desired_spec. For ESX, VMs and NSX Edges it also passes ids, the list of objects to check. On my build ops_remed_u.sls is the same file. Whether it only assesses or also fixes comes down to Salt’s test flag, as you’ll see below.

Jobs

There are a few ways to look at jobs, and they show different things. job.get_jobs lists saved job definitions. Mine is empty, because VCF Operations doesn’t save its jobs there:

The job history lives under ret.get_jids gives the recent job IDs with the function and the user, and get_jid gives the per-minion return of one job:

Pick a JID from that list to see what each minion returned:

Then there’s ret.get_returns, which is the one I’d point anyone to first. Filter on state.apply and every result includes fun_args, which is the exact pillar VCF Operations sent: object type, object IDs and the full desired spec.

Whenever I wasn’t sure what a call should look like, I found the job VCF Operations ran for the same object type and copied it.

Finding the IDs you need

Two kinds of ID go into every compliance call for ESX: the vCenter minion ID and one or more ESX host IDs. I got both wrong at least once, so here’s where they come from.

The vCenter minion ID

Minion IDs are UUIDs, not host names. In my lab, the vCenter minion ID is the same value as its vcfops_resource_id grain. The quickest way to find it is the grain filter from the previous section, which gives one line per vCenter:

It’s used in two places:

  • as the target of the job: "tgt": {"*": {"tgt": "L@<vcenter-minion-id>", "tgt_type": "compound"}},
  • as the key of the pillar’s object_id map, with the ESX hosts as its value.

Because object_id is a map, one call can cover several vCenters: one key per vCenter minion, each with its own list of hosts. The state looks up its own minion ID in that map, so every minion you target needs a key there.

ESX host IDs

This is where I lost the most time. My first guess was the managed object ID from the vCenter URL, host-25 for esx214. The assessment came back with an overall COMPLIANT and I was pleased for about a minute, until I looked at the per-host result:

For ESX, the IDs are the host names as vCenter and VCF Operations know them. In my lab those are the FQDNs, esx213.set.lab to esx218.set.lab. VMs are different again: for them VCF Operations sends managed object IDs like vm-31. The quickest way to get the host list is, once more, a job VCF Operations already ran:

(The max_by picks the job with the most settings in its spec, so it’s one of VCF Operations’ runs and not one of your own test runs.)

Also note that a skipped host doesn’t fail the run. Always read the per-host status, not only the summary.

What VCF Operations sends per object type

Here’s what I found in the job history for each object type, in case you want to go beyond ESX. The grain kind and the object type don’t always use the same name.

object_typeGrain kindKey in object_idIDs in the list
esxi(no minion)vCenter minionESX host names (FQDNs)
vm(no minion)vCenter minionVM managed object IDs (vm-NNN)
vcentervcentervCenter minionthe vCenter minion ID
nsxt_managernsxmeach NSX Manager minionthat minion’s ID
sddc_managersddcmSDDC Manager minionthat minion’s ID
vropsopseach VCF Operations node minionthat minion’s ID
cpcloud-proxycloud proxy minionthat minion’s ID
vmspvmspVSP salt-minion podthat minion’s ID
vcfa(no minion)VSP salt-minion poda UUID (I assume the VCF Automation instance)

Drafting the NTP spec

For the desired values I used the Salt for VCF Component State & Jinja File Generator from Salt for VCF github page. You pick a component, pick configuration IDs from a list and fill in the values, and it gives you a Salt state file and a Jinja file with the settings. It saved me a lot of digging, mostly because it lists every configuration ID per component with its title and a sensible starting value.

For ESX, NTP is three configuration IDs:

  • 147: ESX host must configure NTP time synchronization.
  • 148: The ESX host must configure NTP Service startup policy.
  • 149: Start NTP service on the ESX host.

I started with 147 alone and two NTP servers.

Before pushing, I changed two names, so the spec matches what the compliance module expects. It is also the shape VCF Operations sends:

  1. The top-level key. The generator uses state_to_jinja_linked_name as the link between the state and the Jinja file. check_control wants compliance_config there, with the product (esxi) one level below it. With any other name the run ends with Desired spec is empty or not in correct format.
  2. The setting name. The module has its own names for settings, and the one for 147 is ntp_config. The easiest place to find them is a VCF Operations assessment job:

For NTP, that gives these names. The values are the ones I want:

Config IDSetting nameValue
147ntp_config{“protocol”: “ntp”, “servers”: [“10.3.0.10”, “10.3.1.10”]}
148ntp_service_startup_policy{“service_policy”: “on”}
149ntp_service_config{“service_running”: true}

protocol is required for ntp_config. If you leave it out, the host reports a validation error. The generator already fills it in.

Here are the two files I ended up with. The Jinja file holds the spec:

The state file loads it and passes it to the compliance check:

Push the files

Files go to the Salt file server with fs.save_file. The docs say text files can be sent as plain text, but my build base64-decodes whatever arrives in contents. A plain-text upload got me a 5000 and a binascii.Error in the RaaS log. So I base64-encode everything:

Assess

For the actual run, I went back to the built-in states, ops_assess_u and ops_remed_u, and passed my spec through the pillar, the same way VCF Operations does. The generated state doesn’t pass ids, which ESX needs. The Jinja file is still where I keep the spec; yq turns its YAML into the JSON for the pillar:

If you’d rather not use jq, this is the request it builds (host list shortened):

A few things in there:

  • test: true is Salt’s dry run, so check_control only reports.
  • queue: true queues the run if another state run is already going on the minion, which can happen while VCF Operations is doing its own.
  • object_id maps the vCenter minion to its ESX hosts, and tgt sends the job to that same minion.

Send it, wait a little, and fetch the return:

The return is long. These two filters give the per-host status and the NTP details:

All six came back NON_COMPLIANT, and for esx214 the second filter showed exactly what I expected, one server configured and two wanted:

The state itself reports result: null, which is Salt’s way of saying “this would change” in test mode.

Remediate

Remediation is the same request with ops_remed_u and without test. Run the assessment first and read it, because this one changes your hosts.

The same two filters, run on remediate_result.json, show SUCCESS for all six hosts and the server list before and after:

In vCenter, esx214 now lists both servers and the NTP service is running.

Finally, run the assessment again. Every host should come back COMPLIANT for ntp_config.

Things that tripped me up

  • A skipped host still gives an overall COMPLIANT. Read the per-host status.
  • ESX IDs are host names (FQDNs in my lab), not host-NNN. VM IDs, on the other hand, are vm-NNN.
  • The files live in vcfsecops. Pass saltenv to state.apply, or Salt looks in base.
  • On my build, fs.save_file wanted base64 for .jinja and .sls files.
  • Setting names are fixed by the module (ntp_config, not ntp). Copy them from a VCF Operations job.
  • The API docs follow the latest release. On my build, tgt.get_tgts, test.ping and stats.get_stats return 3001. The upload and run snippet on the generator page uses fs.write_file and job.run, while my build has fs.save_file and cmd.route_cmd. Check what yours has; a 3004 means the method is there.
  • Grain kinds and object types use different names (nsxm and nsxt_manager, ops and vrops).
  • When something returns 5000, the RaaS pod log has the real error.

What’s next

Next I’ll add 148 and 149 to the same spec, so the NTP service is set to start with the host and is running, and then work through the other ESX settings that come back non-compliant. 

Because object_id is a map, the same request will cover more than one vCenter once there’s a second one in the lab.

References


Discover more from VMware Cloud Foundation (VCF) Blog

Subscribe to get the latest posts sent to your email.