In this case though, Weiner did not "literally name" the informant in The New York Times article [1]. He referred to the informant as a "retired terrorist" without giving a name.
In general, you're right about the principle of keeping sources anonymous to avoid harm. One principle in journalism ethics [2] is to "identify sources clearly" so the public can better understand a source's motives. But the other is to minimize harm ("Balance the public’s need for information against potential harm or discomfort.") to both the sources and the public, motivating anonymity in certain cases.
However, in this instance, Weiner didn't name the informant in the article.
In general, you're right about the principle of keeping sources anonymous to avoid harm. One principle in journalism ethics [2] is to "identify sources clearly" so the public can better understand a source's motives. But the other is to minimize harm ("Balance the public’s need for information against potential harm or discomfort.") to both the sources and the public, motivating anonymity in certain cases.
However, in this instance, Weiner didn't name the informant in the article.
[1] https://www.nytimes.com/1995/08/21/world/cia-re-examines-hir...
[2] This is just one ethical code, but many ethical codes are essentially very similar: https://www.spj.org/ethicscode.asp