Edits history of script submission #24079 for ' Apply access policy (f5_bigip)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    async function connect(auth: RT.F5Bigip): Promise<{
      base: string
      headers: { [key: string]: string }
      release: () => Promise<void>
    }> {
      const base = auth.base_url.replace(/\/+$/, "")
      if (auth.auth_method === "basic") {
        return {
          base,
          headers: {
            Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
          },
          release: async () => {},
        }
      }
      const login = await fetch(`${base}/mgmt/shared/authn/login`, {
        method: "POST",
        headers: { "Content-Type": "application/json", Accept: "application/json" },
        body: JSON.stringify({
          username: auth.username,
          password: auth.password,
          loginProviderName: auth.login_provider_name || "tmos",
        }),
      })
      if (!login.ok) {
        throw new Error(`${login.status} ${await login.text()}`)
      }
      const token: string = (await login.json()).token.token
      return {
        base,
        headers: { "X-F5-Auth-Token": token },
        release: async () => {
          await fetch(
            `${base}/mgmt/shared/authz/tokens/${encodeURIComponent(token)}`,
            {
              method: "DELETE",
              headers: { "X-F5-Auth-Token": token },
            }
          )
        },
      }
    }
    
    // "/Common/web" or "web" + partition -> "~Common~web" (iControl REST object reference);
    // "%" (route domains, e.g. 10.0.0.1%2:80), "?" and "#" are escaped
    function ref(name: string, partition: string | undefined) {
      const path = name.startsWith("/") ? name : `/${partition || "Common"}/${name}`
      return path.replace(/\//g, "~").replace(/[%?#]/g, encodeURIComponent)
    }
    
    /**
     * Apply access policy
     * Apply the access policy of an APM access profile (the "Apply Access Policy" action of the BIG-IP UI), by incrementing the profile's generation. New sessions use the updated policy.
     */
    export async function main(
      auth: RT.F5Bigip,
      access_profile: string,
      partition: string | undefined
    ) {
      const f5 = await connect(auth)
      try {
        const response = await fetch(
          `${f5.base}/mgmt/tm/apm/profile/access/${ref(access_profile, partition)}`,
          {
            method: "PATCH",
            headers: {
              ...f5.headers,
              "Content-Type": "application/json",
              Accept: "application/json",
            },
            body: JSON.stringify({ generationAction: "increment" }),
          }
        )
    
        if (!response.ok) {
          throw new Error(`${response.status} ${await response.text()}`)
        }
    
        return await response.json()
      } finally {
        await f5.release()
      }
    }
    

    Submitted by hugo989 7 hours ago