ALMATY, Kazakhstan—In a top-secret operation earlier this year, Kazakh counterintelligence officers swooped in on a Soviet-era apartment block and detained a senior government adviser on charges of spying for China.

Months later, the authorities did something unusual. They allowed information about the case to leak in local media, a rare instance of open push back against Beijing’s growing influence in Central Asia’s largest and richest country.

The arrest of Konstantin Syroyezhkin—a former Soviet KGB agent accused of passing classified documents to Chinese agents, according to people with knowledge of the investigation—comes as Kazakhstan’s leaders struggle to balance a hunger for Chinese investment with fears of encroachment by their giant eastern neighbor.

“By making this story public, the Kazakhs are sending China a message—not to get too bold in Kazakhstan or go too far,” said Vasily Kashin, a China expert at Moscow’s Higher School of Economics.

For decades after the breakup of the Soviet Union, Kazakhstan’s leaders have tried to juggle relationships with the West along with ties to their old paymasters in Moscow. But in recent years China has emerged as a new and powerful player in what is now a three-way balancing act.