If you want to color each face in a different color and you want to find a in-shader solution, the a possibility would be to color the fragments of the cube, dependent on the component of the vertex coordinate with the maximum amount.
Pass the vertex coordinate to the fragment shader:
#version 330
uniform mat4 u_m_matrix;
uniform mat4 u_vp_matrix;
layout (location=0) in vec3 a_position;
//layout (location=1) in vec3 a_normal;
out vec3 vertPos;
void main()
{
vertPos = a_position;
gl_Position = u_vp_matrix * u_m_matrix * vec4(a_position, 1.0);
}
Find the component of the vertex coordinate withe maximum absolut value and chose a color:
#version 330
in vec3 vertPos;
out vec4 fragColor;
void main() {
vec3 posAbs = abs(vertPos);
vec3 color = step(posAbs.yzx, posAbs) * step(posAbs.zxy, posAbs);
color += (1.0 - step(color.zxy * vertPos.zxy, vec3(0.0)));
fragColor = vec4(color, 1.0);
}

If the normal vectors are face normals, then there is even a simpler solution using the normal vectors:
Pass the normal vector to the fragment shader:
#version 330
uniform mat4 u_m_matrix;
uniform mat4 u_vp_matrix;
layout (location=0) in vec3 a_position;
layout (location=1) in vec3 a_normal;
out vec3 normal;
void main()
{
normal = a_normal;
gl_Position = u_vp_matrix * u_m_matrix * vec4(a_position, 1.0);
}
Compute the color dependent on the normal vector:
#version 330
in vec3 normal;
out vec4 fragColor;
void main() {
vec3 color = abs(normal.xyz) + max(normal.zxy, 0.0);
fragColor = vec4(color, 1.0);
}
[...] so I need 24 colors. [...]
In that case I suggest the following solution.
#version 330
in vec3 vertPos;
out vec4 fragColor;
void main() {
vec3 posAbs = abs(vertPos);
vec3 color = (step(posAbs.yzx, posAbs) * step(posAbs.zxy, posAbs) +
step(0.0, vertPos.yzx)) * 0.5;
fragColor = vec4(color, 1.0);
}
