This question is inspired by this physics stack exchange post, which is recent and has not received an answer yet, nontheless I feel that there is a better way to ask this question here with a larger scope than the OP did there.

At first I am primarily interested in the local question so let $X$ be an $n$ dimensional smooth real manifold that if necessary might be assumed to be contractible or otherwise topological trivial.

Let us fix a Lie algebra $\mathfrak g$, let $\Omega^k(X,\mathfrak g)$ denote the module of smooth $\mathfrak g$-valued $k$-forms.

The following is known. Let us define $C:\Omega^1(X,\mathfrak g)\rightarrow \Omega^2(X,\mathfrak g)$ by $$ C(\omega)=d\omega+\frac{1}{2}[\omega\wedge\omega], $$ and call $C$ the curvature operator. It is known that at least locally $C(\omega)=0$ if and only if there is a function $f\in C^\infty(X,G)$ (where $G$ is a/the Lie group associated with $\mathfrak g$) such that $$ \omega=f^\ast\Xi_G\equiv \Delta_Gf, $$ where $\Xi_G\in\Omega^1(G,\mathfrak g)$ is the Maurer-Cartan form of $G$. I have taken the liberty of using $\Delta_G$ for this operation (nonstandard notation) and iirc this is sometimes referred to as the *Darboux-derivative* (treated in Sharpe for example).

Now, let $F=C(\omega)=d\omega+\frac{1}{2}[\omega\wedge\omega]\in\Omega^2(X,\mathfrak g)$. Such curvature forms satisfy the (differential) Bianchi identity $$ d_\omega F=dF+[\omega\wedge F]=0. $$

It this seems that one may define a cochain complex-like structure $$ 0\longrightarrow C^\infty(X,G)\longrightarrow^{\Delta_G}\Omega^1(X,\mathfrak g)\longrightarrow^{C}\Omega^2(X,\mathfrak g)\longrightarrow^{d_\omega}\Omega^3(X,\mathfrak g) ... $$ in that the composition of two subsequent arrows always return $0$, however this is not a true cochain complex because for example $C^\infty(X,G)$ is not an Abelian group (if $G$ is nonabelian).

What I am primarily interested in is whether it is possible to define such a sequence rigorously in some way which satisfies a local exactness property. This sequence is locally exact at $\Omega^1(X,\mathfrak g)$, which can be verified using the Frobenius integrability theorem, but - in my opinion - a more interesting question is the $\Omega^2(X,\mathfrak g)$.

Specifically what is the necessary and sufficient (local) condition for a Lie algebra valued $2$-form to be the curvature form of a connexion? Even if the Bianchi identity is a complete local integrability condition, it is formulated in terms of $2$-forms so Frobenius' theorem does not apply, moreover in order to calculate the covariant exterior derivative of the curvature $2$-form one already needs to know the connection form as well.

If a necessary and sufficient condition can be given, is there an explicit "homotopy operator" that allows one to construct a connection form from a given curvature form?

*Remark:** In $\dim X=3$ the curvature form is the Euler-Lagrange form of the Chern-Simons Lagrangian, so I guess methods of the variational bicomplex could be used to attack this problem. As far as I see this only works in three dimensions though.